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