Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* lexical.h -- version 0.2, May 1st, 2015
- Copyright (C) 2015 Raphaël Dujardin
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- Raphaël Dujardin
- rdujardin.com
- */
- #ifndef LEXICAL_H
- #define LEXICAL_H
- #include <vector>
- #include <deque>
- #include <string>
- #include <iostream>
- #include <utility>
- namespace lex
- {
- //Définition des types de tokens existants en JavaScript
- enum TokenType
- {
- NULLTOKEN, //pour signifier une erreur de token inexistant
- INTEGER, //tokens à valeur, un nom est un identifiant (à ne pas confondre avec une chaîne de caractères)
- FLOATING,
- STRING,
- NAME,
- PLUS, //opérateurs
- MINUS,
- MULTIPLY,
- DIVIDE,
- MODULO,
- EQUAL,
- DBPLUS,
- DBMINUS,
- LPARENTHESIS, //délimiteurs
- RPARENTHESIS,
- LBRACKET,
- RBRACKET,
- LSQUARE,
- RSQUARE,
- COMMA, //ponctuations
- SEMICOLON,
- BREAK, //mots-clés
- CASE,
- CATCH,
- CONST,
- CONTINUE,
- DEBUGGER,
- DO,
- ELSE,
- FINALLY,
- FOR,
- FUNCTION,
- IF,
- IN,
- INSTANCEOF,
- LET,
- NEW,
- RETURN,
- SWITCH,
- THIS,
- THROW,
- TRY,
- TYPEOF,
- VAR,
- VOID,
- WHILE,
- INFERIOR, //opérateurs de comparaison
- SUPERIOR,
- DBEQUAL
- };
- //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
- class Token
- {
- public:
- Token(TokenType _type=NULLTOKEN,std::string _value="");
- Token(const Token& token);
- ~Token();
- void print(std::ostream& out,bool withValue=true,bool withEol=false) const;
- void printType(std::ostream& out) const;
- void printCode(std::ostream& out) const;
- Token& operator=(const Token &token);
- TokenType type;
- std::string value;
- };
- //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.)
- //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
- class Sequence
- {
- public:
- Sequence();
- Sequence(const std::string &code);
- Sequence(const Token& token);
- Sequence(const Sequence& sequence);
- ~Sequence();
- void print(std::ostream& out,bool compact=true) const;
- void printCode(std::ostream& out) const;
- void lex(const std::string& code);
- Sequence& operator=(const Sequence &sequence);
- Sequence& operator+=(const Sequence &sequence);
- Sequence& operator+=(const Token &token);
- std::deque<Token> tokens;
- private:
- std::string toStr(const std::vector<char> &buf);
- std::pair<TokenType,std::vector<char>::const_iterator> numberBuf(const std::vector<char> &buf);
- Sequence analyzeBuf(const std::vector<char> &buf);
- };
- }
- std::ostream& operator<<(std::ostream& stream,const lex::TokenType &tokenType);
- std::ostream& operator<<(std::ostream& stream,const lex::Token& token);
- std::ostream& operator<<(std::ostream& stream,const lex::Sequence& sequence);
- lex::Sequence operator+(const lex::Sequence &seq1,const lex::Sequence &seq2);
- lex::Sequence operator+(const lex::Sequence &seq,const lex::Token &token);
- lex::Sequence operator+(const lex::Token &token1,const lex::Token &token2);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement