Advertisement
rdujardin

C++ Lexical Analyser for JavaScript : lexical.h

May 1st, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. /* lexical.h -- version 0.2, May 1st, 2015
  2.  
  3.   Copyright (C) 2015 Raphaël Dujardin
  4.  
  5.   This software is provided 'as-is', without any express or implied
  6.   warranty.  In no event will the authors be held liable for any damages
  7.   arising from the use of this software.
  8.  
  9.   Permission is granted to anyone to use this software for any purpose,
  10.   including commercial applications, and to alter it and redistribute it
  11.   freely, subject to the following restrictions:
  12.  
  13.   1. The origin of this software must not be misrepresented; you must not
  14.      claim that you wrote the original software. If you use this software
  15.      in a product, an acknowledgment in the product documentation would be
  16.      appreciated but is not required.
  17.   2. Altered source versions must be plainly marked as such, and must not be
  18.      misrepresented as being the original software.
  19.   3. This notice may not be removed or altered from any source distribution.
  20.  
  21.   Raphaël Dujardin
  22.   rdujardin.com
  23.  
  24. */
  25.  
  26. #ifndef LEXICAL_H
  27. #define LEXICAL_H
  28.  
  29. #include <vector>
  30. #include <deque>
  31. #include <string>
  32. #include <iostream>
  33. #include <utility>
  34.  
  35. namespace lex
  36. {
  37.  
  38.     //Définition des types de tokens existants en JavaScript
  39.     enum TokenType
  40.     {
  41.         NULLTOKEN, //pour signifier une erreur de token inexistant
  42.  
  43.         INTEGER, //tokens à valeur, un nom est un identifiant (à ne pas confondre avec une chaîne de caractères)
  44.         FLOATING,
  45.         STRING,
  46.         NAME,
  47.  
  48.         PLUS, //opérateurs
  49.         MINUS,
  50.         MULTIPLY,
  51.         DIVIDE,
  52.         MODULO,
  53.         EQUAL,
  54.         DBPLUS,
  55.         DBMINUS,
  56.  
  57.         LPARENTHESIS, //délimiteurs
  58.         RPARENTHESIS,
  59.         LBRACKET,
  60.         RBRACKET,
  61.         LSQUARE,
  62.         RSQUARE,
  63.  
  64.         COMMA, //ponctuations
  65.         SEMICOLON,
  66.  
  67.         BREAK, //mots-clés
  68.         CASE,
  69.         CATCH,
  70.         CONST,
  71.         CONTINUE,
  72.         DEBUGGER,
  73.         DO,
  74.         ELSE,
  75.         FINALLY,
  76.         FOR,
  77.         FUNCTION,
  78.         IF,
  79.         IN,
  80.         INSTANCEOF,
  81.         LET,
  82.         NEW,
  83.         RETURN,
  84.         SWITCH,
  85.         THIS,
  86.         THROW,
  87.         TRY,
  88.         TYPEOF,
  89.         VAR,
  90.         VOID,
  91.         WHILE,
  92.        
  93.         INFERIOR, //opérateurs de comparaison
  94.         SUPERIOR,
  95.         DBEQUAL
  96.     };
  97.  
  98.     //La classe Token décrit un token, caractérisé par un type de type énuméré TokenType et par une valeur de type chaîne de caractères
  99.     class Token
  100.     {
  101.     public:
  102.         Token(TokenType _type=NULLTOKEN,std::string _value="");
  103.         Token(const Token& token);
  104.         ~Token();
  105.        
  106.         void print(std::ostream& out,bool withValue=true,bool withEol=false) const;
  107.         void printType(std::ostream& out) const;
  108.         void printCode(std::ostream& out) const;
  109.        
  110.         Token& operator=(const Token &token);
  111.        
  112.         TokenType type;
  113.         std::string value;
  114.     };
  115.    
  116.     //La classe Sequence décrit une séquence de tokens, et fournit des facilités de manipulation de ces séquences (concaténation, copie, affichage, etc.)
  117.     //La méthode lex effectue l'analyse lexicale d'un code JavaScript fourni en paramètre, cette analyse peut également être appelée en fournissant le code au constructeur
  118.     class Sequence
  119.     {
  120.     public:
  121.         Sequence();
  122.         Sequence(const std::string &code);
  123.         Sequence(const Token& token);
  124.         Sequence(const Sequence& sequence);
  125.         ~Sequence();
  126.        
  127.         void print(std::ostream& out,bool compact=true) const;
  128.         void printCode(std::ostream& out) const;
  129.        
  130.         void lex(const std::string& code);
  131.        
  132.         Sequence& operator=(const Sequence &sequence);
  133.         Sequence& operator+=(const Sequence &sequence);
  134.         Sequence& operator+=(const Token &token);
  135.        
  136.         std::deque<Token> tokens;
  137.        
  138.     private:
  139.         std::string toStr(const std::vector<char> &buf);
  140.         std::pair<TokenType,std::vector<char>::const_iterator> numberBuf(const std::vector<char> &buf);
  141.         Sequence analyzeBuf(const std::vector<char> &buf);
  142.     };
  143.  
  144. }
  145.  
  146. std::ostream& operator<<(std::ostream& stream,const lex::TokenType &tokenType);
  147. std::ostream& operator<<(std::ostream& stream,const lex::Token& token);
  148. std::ostream& operator<<(std::ostream& stream,const lex::Sequence& sequence);
  149. lex::Sequence operator+(const lex::Sequence &seq1,const lex::Sequence &seq2);
  150. lex::Sequence operator+(const lex::Sequence &seq,const lex::Token &token);
  151. lex::Sequence operator+(const lex::Token &token1,const lex::Token &token2);
  152.  
  153. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement