Class: TSJSON::Parser
- Inherits:
-
Object
- Object
- TSJSON::Parser
- Defined in:
- lib/language/parser/parser.rb
Instance Method Summary collapse
-
#current_token ⇒ Object
Returns current token from lexer.
-
#expect_optional_token(kind) ⇒ Object
If the next token is of the given kind, return that token after advancing the lexer.
-
#expect_token(kind) ⇒ Object
If the next token is of the given kind, return that token after advancing the lexer.
-
#initialize(source) ⇒ Parser
constructor
A new instance of Parser.
-
#loc(start_token) ⇒ Object
Returns a location object, used to identify the place in the source that created a given parsed object.
- #parse_definition ⇒ Object
- #parse_document ⇒ Object
- #parse_enum_definition ⇒ Object
- #parse_enum_member ⇒ Object
- #parse_float ⇒ Object
- #parse_int ⇒ Object
-
#parse_many(open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil) ⇒ Object
Returns a non-empty list of parse nodes, determined by the parseFn.
- #parse_name ⇒ Object
- #parse_operation ⇒ Object
-
#parse_optional_many(open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil) ⇒ Object
Returns a list of parse nodes, determined by the parseFn.
- #parse_parenthesized_type ⇒ Object
- #parse_property_signature ⇒ Object
- #parse_string_literal ⇒ Object
- #parse_tuple ⇒ Object
- #parse_type_alias_definition ⇒ Object
- #parse_type_definition ⇒ Object
- #parse_type_literal ⇒ Object
- #parse_type_parameter ⇒ Object
- #parse_type_reference ⇒ Object
-
#peek(kind) ⇒ Object
Determines if the next token is of a given kind.
-
#unexpected(at_token = nil) ⇒ Object
Helper function for creating an error when an unexpected lexed token is encountered.
Constructor Details
Instance Method Details
#current_token ⇒ Object
Returns current token from lexer
345 346 347 |
# File 'lib/language/parser/parser.rb', line 345 def current_token @lexer.token end |
#expect_optional_token(kind) ⇒ Object
If the next token is of the given kind, return that token after advancing the lexer. Otherwise, do not change the parser state and return undefined.
428 429 430 431 432 433 434 435 |
# File 'lib/language/parser/parser.rb', line 428 def expect_optional_token(kind) token = current_token if token.kind === kind @lexer.advance return token end return nil end |
#expect_token(kind) ⇒ Object
If the next token is of the given kind, return that token after advancing the lexer. Otherwise, do not change the parser state and throw an error.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/language/parser/parser.rb', line 410 def expect_token(kind) token = current_token if (token.kind == kind) @lexer.advance return token end raise TSJSONSyntaxError.syntax_error( @lexer.source, token.start_pos, "Expected #{LexerUtils.get_token_kind_desc(kind)}, found #{ LexerUtils.get_token_desc(token) }." ) end |
#loc(start_token) ⇒ Object
Returns a location object, used to identify the place in the source that created a given parsed object.
362 363 364 365 |
# File 'lib/language/parser/parser.rb', line 362 def loc(start_token) end_token = @lexer.last_token return {} #Location.new(start_token, end_token, @lexer.source) end |
#parse_definition ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/language/parser/parser.rb', line 27 def parse_definition if (peek(TokenKind::NAME)) case current_token.value when 'type' return parse_type_alias_definition when 'enum' return parse_enum_definition end end raise unexpected end |
#parse_document ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/language/parser/parser.rb', line 13 def parse_document return( { kind: AST::Kind::Document, definitions: parse_optional_many( TokenKind::SOF, :parse_definition, TokenKind::EOF ) } ) end |
#parse_enum_definition ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/language/parser/parser.rb', line 308 def parse_enum_definition start = expect_token(TokenKind::NAME) name = parse_name members = parse_many( TokenKind::BRACE_L, :parse_enum_member, TokenKind::BRACE_R, TokenKind::COMMA ) expect_optional_token(TokenKind::SEMICOLON) return( { kind: AST::Kind::Enum, name: name, members: members, loc: loc(start) } ) end |
#parse_enum_member ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/language/parser/parser.rb', line 325 def parse_enum_member start = current_token name = parse_name value = nil value = parse_string_literal if (expect_optional_token(TokenKind::EQUALS)) return( { kind: AST::Kind::EnumMember, name: name, value: value, loc: loc(start) } ) end |
#parse_float ⇒ Object
225 226 227 228 229 230 |
# File 'lib/language/parser/parser.rb', line 225 def parse_float() token = expect_token(TokenKind::FLOAT) return( { kind: AST::Kind::Float, value: token.value.to_f, loc: loc(token) } ) end |
#parse_int ⇒ Object
220 221 222 223 |
# File 'lib/language/parser/parser.rb', line 220 def parse_int() token = expect_token(TokenKind::INT) return { kind: AST::Kind::Int, value: token.value.to_i, loc: loc(token) } end |
#parse_many(open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil) ⇒ Object
Returns a non-empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
375 376 377 378 379 380 381 382 383 384 |
# File 'lib/language/parser/parser.rb', line 375 def parse_many(open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil) expect_token(open_kind) nodes = [] loop do nodes.push(self.send(parse_fn_symbol)) expect_token(delimeter_kind) if (delimeter_kind && !peek(close_kind)) break if expect_optional_token(close_kind) end return nodes end |
#parse_name ⇒ Object
75 76 77 78 |
# File 'lib/language/parser/parser.rb', line 75 def parse_name token = expect_token(TokenKind::NAME) return { kind: AST::Kind::Name, value: token.value, loc: loc(token) } end |
#parse_operation ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/language/parser/parser.rb', line 140 def parse_operation expect_optional_token(TokenKind::PIPE) nodes = [parse_type_definition] operations = [] execute_operation = lambda do operation = operations.shift right = nodes.pop left = nodes.pop new_operation = nil if (operation == TokenKind::AMP) if (left[:kind] == AST::Kind::IntersectionType) new_operation = { kind: AST::Kind::IntersectionType, types: left[:types].concat([right]) } else new_operation = { kind: AST::Kind::IntersectionType, types: [left, right] } end elsif (operation == TokenKind::PIPE) if (left[:kind] == AST::Kind::UnionType) new_operation = { kind: AST::Kind::UnionType, types: left[:types].concat([right]) } else new_operation = { kind: AST::Kind::UnionType, types: [left, right] } end end nodes.push(new_operation) end operation_kind = nil loop do unless ( LexerUtils.is_operation_token( (operation_kind = current_token.kind) ) ) break end loop do unless ( operations.length > 0 && TokenKind.operation_precedence(operations[0]) >= TokenKind.operation_precedence(operation_kind) ) break end execute_operation.call end operations.unshift(operation_kind) @lexer.advance nodes.push(parse_type_definition) end loop do break unless (operations.length > 0) execute_operation.call end return nodes[0] end |
#parse_optional_many(open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil) ⇒ Object
Returns a list of parse nodes, determined by the parseFn. It can be empty only if open token is missing otherwise it will always return non-empty list that begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/language/parser/parser.rb', line 390 def parse_optional_many( open_kind, parse_fn_symbol, close_kind, delimeter_kind = nil ) if (expect_optional_token(open_kind)) nodes = [] loop do break if expect_optional_token(close_kind) nodes.push(self.send(parse_fn_symbol)) expect_token(delimeter_kind) if (delimeter_kind && !peek(close_kind)) end return nodes end return [] end |
#parse_parenthesized_type ⇒ Object
301 302 303 304 305 306 |
# File 'lib/language/parser/parser.rb', line 301 def parse_parenthesized_type() start = expect_token(TokenKind::PAREN_L) type = parse_operation expect_token(TokenKind::PAREN_R) return { kind: AST::Kind::ParenthesizedType, type: type, loc: loc(start) } end |
#parse_property_signature ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/language/parser/parser.rb', line 282 def parse_property_signature() start = current_token name = parse_name optional = expect_optional_token(TokenKind::QUESTION_MARK).nil?.! expect_token(TokenKind::COLON) type = parse_operation expect_token(TokenKind::SEMICOLON) return( { kind: AST::Kind::PropertySignature, name: name, type: type, optional: optional, loc: loc(start) } ) end |
#parse_string_literal ⇒ Object
213 214 215 216 217 218 |
# File 'lib/language/parser/parser.rb', line 213 def parse_string_literal() token = expect_token(TokenKind::STRING) return( { kind: AST::Kind::StringLiteral, value: token.value, loc: loc(token) } ) end |
#parse_tuple ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/language/parser/parser.rb', line 253 def parse_tuple start = current_token types = parse_many( TokenKind::BRACKET_L, :parse_type_definition, TokenKind::BRACKET_R, TokenKind::COMMA ) return { kind: AST::Kind::Tuple, types: types, loc: loc(start) } end |
#parse_type_alias_definition ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/language/parser/parser.rb', line 40 def parse_type_alias_definition start = expect_token(TokenKind::NAME) name = parse_name parameters = parse_optional_many( TokenKind::CHEVRON_L, :parse_type_parameter, TokenKind::CHEVRON_R, TokenKind::COMMA ) expect_token(TokenKind::EQUALS) definition = parse_operation expect_optional_token(TokenKind::SEMICOLON) return( { kind: AST::Kind::TypeAlias, name: name, parameters: parameters, definition: definition, loc: loc(start) } ) end |
#parse_type_definition ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/language/parser/parser.rb', line 80 def parse_type_definition() start = current_token type = nil if (peek(TokenKind::NAME)) type = parse_type_reference elsif (peek(TokenKind::BRACE_L)) type = parse_type_literal elsif (peek(TokenKind::PAREN_L)) type = parse_parenthesized_type elsif (peek(TokenKind::BRACKET_L)) type = parse_tuple elsif (peek(TokenKind::STRING)) type = parse_string_literal elsif (peek(TokenKind::INT)) type = parse_int elsif (peek(TokenKind::FLOAT)) type = parse_float end throw unexpected if (!type) loop do unless [TokenKind::BRACKET_L, TokenKind::DOT].include?( current_token.kind ) break end if (expect_optional_token(TokenKind::BRACKET_L)) if (expect_optional_token(TokenKind::BRACKET_R)) type = { kind: AST::Kind::ArrayType, type: type, loc: loc(start) } else index = parse_string_literal expect_token(TokenKind::BRACKET_R) type = { kind: AST::Kind::IndexAccess, target: type, index: index, loc: loc(start) } end elsif (expect_optional_token(TokenKind::DOT)) property = parse_name type = { kind: AST::Kind::PropertyAccess, target: type, property: property, loc: loc(start) } end end if (expect_optional_token(TokenKind::BRACKET_L)) expect_token(TokenKind::BRACKET_R) type = { kind: AST::Kind::ArrayType, type: type, loc: loc(start) } end return type end |
#parse_type_literal ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/language/parser/parser.rb', line 265 def parse_type_literal start = current_token properties = parse_optional_many( TokenKind::BRACE_L, :parse_property_signature, TokenKind::BRACE_R ) return( { kind: AST::Kind::TypeLiteral, properties: properties, loc: loc(start) } ) end |
#parse_type_parameter ⇒ Object
68 69 70 71 72 73 |
# File 'lib/language/parser/parser.rb', line 68 def parse_type_parameter start = current_token name = parse_name return { kind: AST::Kind::TypeParameter, name: name, loc: loc(start) } end |
#parse_type_reference ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/language/parser/parser.rb', line 232 def parse_type_reference start = current_token name = parse_name args = parse_optional_many( TokenKind::CHEVRON_L, :parse_operation, TokenKind::CHEVRON_R, TokenKind::COMMA ) return( { kind: AST::Kind::TypeReference, name: name, args: args, loc: loc(start) } ) end |
#peek(kind) ⇒ Object
Determines if the next token is of a given kind
368 369 370 |
# File 'lib/language/parser/parser.rb', line 368 def peek(kind) current_token.kind == kind end |
#unexpected(at_token = nil) ⇒ Object
Helper function for creating an error when an unexpected lexed token is encountered.
350 351 352 353 354 355 356 357 358 359 |
# File 'lib/language/parser/parser.rb', line 350 def unexpected(at_token = nil) token = at_token || current_token return( TSJSONSyntaxError.syntax_error( @lexer.source, token.start_pos, "Unexpected #{LexerUtils.get_token_desc(token)}." ) ) end |