Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | // All rights reserved. This program and the accompanying materials |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #ifndef _Asn_TokenBuf_HH | |
9 | #define _Asn_TokenBuf_HH | |
10 | ||
11 | #include "../vector.hh" | |
12 | ||
13 | #include "AST_asn1.hh" | |
14 | #include "Ref.hh" | |
15 | #include "../Type.hh" | |
16 | #include "../Typestuff.hh" // FIXME CTs | |
17 | #include "../Value.hh" | |
18 | #include "../Valuestuff.hh" | |
19 | #include "Tag.hh" | |
20 | #include "TableConstraint.hh" | |
21 | #include "Block.hh" | |
22 | #include "Object.hh" | |
23 | ||
24 | #include "asn1p.tab.hh" | |
25 | ||
26 | #define yylval asn1_yylval | |
27 | ||
28 | namespace Asn { | |
29 | ||
30 | class TokenBuf; | |
31 | ||
32 | /** | |
33 | * Representation of a token. | |
34 | */ | |
35 | class Token : public Node, public Location { | |
36 | private: | |
37 | int token; | |
38 | union { | |
39 | Identifier *id; | |
40 | int_val_t *i; | |
41 | string *str; | |
42 | Value *value; | |
43 | Block *block; | |
44 | } semval; | |
45 | ||
46 | Token(const Token& p); | |
47 | static bool has_semval(int p_token); | |
48 | public: | |
49 | Token(int p_token, const Location& p_loc); | |
50 | Token(int p_token, const YYSTYPE& p_semval, const Location& p_loc); | |
51 | virtual ~Token(); | |
52 | virtual Token* clone() const; | |
53 | int get_token() const { return token; } | |
54 | void set_token(int new_token); | |
55 | void steal_semval(YYSTYPE& p_semval); | |
56 | void set_loc_info() const; | |
57 | bool is_literal_id() const; | |
58 | bool is_literal_kw() const; | |
59 | bool is_ampId() const; | |
60 | bool is_id() const; | |
61 | static const char* get_token_name(int p_token); | |
62 | const char* get_token_name() const { return get_token_name(token); } | |
63 | const Identifier& get_semval_id() const; | |
64 | /** Transforms \a this (which must be a '{' token) to a block | |
65 | * containing \a tb. */ | |
66 | void create_block(TokenBuf *tb); | |
67 | virtual void dump(unsigned level) const; | |
68 | }; | |
69 | ||
70 | /** | |
71 | * Class to represent group of tokens. An instance of TokenBuf | |
72 | * takes place between yyparse() and yylex(). yyparse() calls | |
73 | * (indirectly) TokenBuf::lex(), which calls (indirectly) | |
74 | * yylex(). TokenBuf::lex() never returns '{'; it buffers the entire | |
75 | * {}-block in a new TokenBuf, and returns a single token which | |
76 | * contains all the tokens (and line number information) in the | |
77 | * block. Then this new TokenBuf can be used to parse the block. | |
78 | */ | |
79 | class TokenBuf : public Node { | |
80 | private: | |
81 | /** Vector type to store tokens. */ | |
82 | typedef vector<Token> tokens_t; | |
83 | ||
84 | /** The Tokens. Not the band. :) */ | |
85 | tokens_t *tokens; | |
86 | /** Name of the file from where the tokens are. */ | |
87 | const char *filename; | |
88 | ||
89 | TokenBuf(const TokenBuf& p); | |
90 | /** Constructs a new TokenBuf with the given tokens. */ | |
91 | TokenBuf(tokens_t *p_tokens); | |
92 | /** Deletes the stored tokens. */ | |
93 | void delete_tokens(); | |
94 | /** Reads (yylex) the next token. Returns true on success, false | |
95 | * on EOF. */ | |
96 | bool read_next(); | |
97 | public: | |
98 | /** Default constructor. */ | |
99 | TokenBuf(); | |
100 | /** Destructor. */ | |
101 | virtual ~TokenBuf(); | |
102 | virtual TokenBuf* clone() const | |
103 | {return new TokenBuf(*this);} | |
104 | const char *get_filename() {return filename;} | |
105 | /** Makes the buffer empty. */ | |
106 | void reset(const char *p_filename); | |
107 | /** Inserts the given token as the first token. */ | |
108 | void push_front_token(Token *p_token); | |
109 | /** Inserts the given keyword token as the first token. */ | |
110 | void push_front_kw_token(int p_kw); | |
111 | /** Inserts the given token as the last token. */ | |
112 | void push_back_token(Token *p_token); | |
113 | /** Inserts the given keyword token as the last token. */ | |
114 | void push_back_kw_token(int p_kw); | |
115 | /** Moves the tokens from \a to to this. */ | |
116 | void move_tokens_from(TokenBuf *tb); | |
117 | /** Removes and returns the first token. */ | |
118 | Token* pop_front_token(); | |
119 | /** Returns a reference to the token in buffer at position \a pos. | |
120 | * Use it carefully. :) */ | |
121 | Token*& get_at(size_t pos); | |
122 | /** Returns a token, sets the corresponding yylineno and | |
123 | * yylval. As needed by yacc/bison. */ | |
124 | int lex(); | |
125 | virtual void dump(unsigned level) const; | |
126 | }; | |
127 | ||
128 | } // namespace Asn | |
129 | ||
130 | #endif /* _Asn_TokenBuf_HH */ |