Class: Luoma::UnifiedParser
- Defined in:
- lib/luoma/parser_unified.rb,
sig/luoma/parser_unified.rbs
Defined Under Namespace
Classes: Precedence
Constant Summary collapse
- PRECEDENCES =
{ token_or_else: Precedence::COALESCE, token_or: Precedence::LOGICAL_OR, token_and: Precedence::LOGICAL_AND, token_not: Precedence::LOGICAL_NOT, token_eq: Precedence::COMPARISON, token_ne: Precedence::COMPARISON, token_lt: Precedence::COMPARISON, token_le: Precedence::COMPARISON, token_gt: Precedence::COMPARISON, token_ge: Precedence::COMPARISON, token_in: Precedence::MEMBERSHIP, token_contains: Precedence::MEMBERSHIP, token_add: Precedence::ADD, token_sub: Precedence::ADD, token_mul: Precedence::MUL, token_div: Precedence::MUL, token_mod: Precedence::MUL, token_pipe: Precedence::PIPE }.freeze
- INFIX_OPERATORS =
{ token_or_else: CoalesceExpression, token_or: OrExpression, token_and: AndExpression, token_eq: EqExpression, token_ne: NeExpression, token_lt: LtExpression, token_le: LeExpression, token_gt: GtExpression, token_ge: GeExpression, token_in: InExpression, token_contains: ContainsExpression, token_add: AddExpression, token_sub: SubExpression, token_mul: MulExpression, token_div: DivExpression, token_mod: ModExpression, token_pipe: nil }.freeze
- TERMINATE_EXPRESSION =
Set[ :token_wc, :token_out_end, :token_tag_end, :token_text, :token_if, :token_else, :token_rparen, :token_eoi, :token_interpolation_end, :token_comma ].freeze
- TERMINATE_FILTER =
Set[ :token_wc, :token_out_end, :token_tag_end, :token_text, :token_if, :token_else, :token_rparen, :token_rbracket, :token_rbrace, :token_pipe, :token_eoi, :token_interpolation_end, :token_or_else, :token_or, :token_and, :token_eq, :token_ne, :token_lt, :token_le, :token_gt, :token_ge, :token_in, :token_contains ].freeze
- PATH_PUNCTUATION =
Set[ :token_dot, :token_lbracket ].freeze
- STRING_LITERAL_KINDS =
Set[ :token_single_escaped, :token_single_quoted, :token_double_escaped, :token_double_quoted ].freeze
- PATH_SEGMENT_KINDS =
Set[ :token_ident, :token_false, :token_true, :token_null, :token_nil, :token_and, :token_or, :token_orElse, :token_not, :token_in, :token_contains ].freeze
- KEYWORD_ARGUMENT_DELIMITERS =
Set[ :token_colon, :token_assign ].freeze
- RE_SLASH_U =
/\\u([0-9a-fA-F]{4})/- RE_ESCAPE_INTERPOLATION =
/\\\$\{/
Instance Attribute Summary
Attributes inherited from Parser
Instance Method Summary collapse
- #high_surrogate?(code_point) ⇒ Boolean
- #low_surrogate?(code_point) ⇒ Boolean
-
#parse_arguments(require_commas: nil) ⇒ Array[Expression | KeywordArgument]
(?require_commas: bool?) -> Array[Expression | KeywordArgument].
-
#parse_array_or_path ⇒ Expression
() -> Expression.
-
#parse_block(stop: nil) ⇒ t_block
(?stop: Set?) -> t_block.
-
#parse_bracketed_segment ⇒ t_path_segment
() -> t_path_segment.
-
#parse_comment ⇒ Markup
() -> Markup.
- #parse_expression(precedence: Precedence::LOWEST) ⇒ Object
-
#parse_false_literal ⇒ Expression
() -> Expression.
-
#parse_filter(token, left) ⇒ FilteredExpression
(t_token, Expression) -> FilteredExpression.
-
#parse_float_literal ⇒ Expression
() -> Expression.
-
#parse_ident ⇒ Name
() -> Name.
-
#parse_infix(left) ⇒ Expression
(Expression) -> Expression.
-
#parse_int_literal ⇒ Expression
() -> Expression.
-
#parse_keyword_arguments(require_commas: nil) ⇒ Array[KeywordArgument]
(?require_commas: bool?) -> Array.
-
#parse_lambda(precedence:) ⇒ Lambda
(precedence: Integer) -> Lambda.
-
#parse_lambda_range_or_group(precedence:) ⇒ Expression
(precedence: Integer) -> Expression.
-
#parse_name ⇒ Name
Parse an identifier, possibly surrounded by quotes.
-
#parse_null_literal ⇒ Expression
() -> Expression.
-
#parse_object_item ⇒ Item, Spread
() -> Item | Spread.
-
#parse_object_literal ⇒ ObjectLiteral
() -> ObjectLiteral.
-
#parse_output ⇒ Markup
() -> Markup.
-
#parse_partial_array(token, first) ⇒ Expression
(t_token, Expression|Spread) -> Expression.
-
#parse_partial_lambda(expr, precedence:) ⇒ Expression
(Expression, precedence: Integer) -> Lambda.
-
#parse_path ⇒ Variable
() -> Variable.
-
#parse_path_segments ⇒ Array[t_path_segment]
() -> Array.
-
#parse_positional_arguments(require_commas: nil) ⇒ Array[Expression]
(?require_commas: bool?) -> Array.
-
#parse_prefix ⇒ Expression
() -> Expression.
-
#parse_primary(precedence: Precedence::LOWEST) ⇒ Expression
(?precedence: Integer) -> Expression.
-
#parse_range_literal ⇒ Expression
() -> Expression.
-
#parse_string_literal ⇒ StringLiteral
() -> StringLiteral.
-
#parse_tag ⇒ Markup
() -> Markup.
-
#parse_ternary(consequence) ⇒ Expression
(Expression) -> Expression.
-
#parse_true_literal ⇒ Expression
() -> Expression.
- #unescape(token) ⇒ String
Methods inherited from Parser
#carry_whitespace_control, #current, #current_value, #eat, #eat_empty_tag, #eat_one_of, #eat_tag, #expect_expression, #initialize, #kind, #next, parse, #peek, #peek_tag_name, #peek_whitespace_control, #skip_whitespace_control, #tag?, #tags
Constructor Details
This class inherits a constructor from Luoma::Parser
Instance Method Details
#high_surrogate?(code_point) ⇒ Boolean
970 971 972 |
# File 'lib/luoma/parser_unified.rb', line 970 def high_surrogate?(code_point) code_point.between?(0xD800, 0xDBFF) end |
#low_surrogate?(code_point) ⇒ Boolean
975 976 977 |
# File 'lib/luoma/parser_unified.rb', line 975 def low_surrogate?(code_point) code_point.between?(0xDC00, 0xDFFF) end |
#parse_arguments(require_commas: nil) ⇒ Array[Expression | KeywordArgument]
(?require_commas: bool?) -> Array[Expression | KeywordArgument]
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/luoma/parser_unified.rb', line 135 def parse_arguments(require_commas: nil) args = [] #: Array[Expression | KeywordArgument] loop do kind_ = kind break if TERMINATE_EXPRESSION.include?(kind_) if kind_ == :token_ident && KEYWORD_ARGUMENT_DELIMITERS.include?(peek.first) # A named argument name = parse_ident eat_one_of(KEYWORD_ARGUMENT_DELIMITERS) args << KeywordArgument.new(name.token, name, parse_expression) else args << parse_expression end kind_ = kind if require_commas && !TERMINATE_EXPRESSION.include?(kind_) eat(:token_comma) elsif kind_ == :token_comma @pos += 1 end end args end |
#parse_array_or_path ⇒ Expression
() -> Expression
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/luoma/parser_unified.rb', line 564 def parse_array_or_path start_pos = @pos token = eat(:token_lbracket) case kind when :token_rbracket # Empty array. ArrayLiteral.new(token, []).with(eat(:token_rbracket)) when :token_triple_dot # An array with a spread operator before the first item. parse_partial_array(token, Spread.new(self.next, parse_expression)) else expr = parse_expression if kind != :token_comma && expr.is_a?(StringLiteral) # A path, backtrack. @pos = start_pos parse_path else parse_partial_array(token, expr) end end end |
#parse_block(stop: nil) ⇒ t_block
(?stop: Set?) -> t_block
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 |
# File 'lib/luoma/parser_unified.rb', line 163 def parse_block(stop: nil) nodes = [] #: t_block loop do token = self.next case token.first when :token_text nodes << @env.trim( Luoma.get_token_value(token, @source), @whitespace_control_carry, peek_whitespace_control ) when :token_out_start nodes << parse_output when :token_tag_start if stop&.include?(peek_tag_name) @pos -= 1 break end nodes << parse_tag when :token_comment_start nodes << parse_comment when :token_eoi break else raise TemplateSyntaxError.new( "unexpected #{Luoma::TOKEN_KIND_MAP[token.first].inspect}", token, @source, @template_name ) end end nodes end |
#parse_bracketed_segment ⇒ t_path_segment
() -> t_path_segment
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/luoma/parser_unified.rb', line 529 def parse_bracketed_segment eat(:token_lbracket) token = self.next case token.first when :token_int IndexSelector.new( token, Luoma.get_token_value(token, @source).to_i ).with(eat(:token_rbracket)) when :token_ident, :token_false, :token_true, :token_null, :token_nil, :token_not, :token_and, :token_or @pos -= 1 path = parse_path eat(:token_rbracket) path when :token_double_quote, :token_single_quote @pos -= 1 parse_string_literal.with(eat(:token_rbracket)) when :token_rbracket raise TemplateSyntaxError.new( "empty bracketed segment", token, @source, @template_name ) else raise TemplateSyntaxError.new( "expected an integer, identifier or string", token, @source, @template_name ) end end |
#parse_comment ⇒ Markup
() -> Markup
440 441 442 443 444 445 446 |
# File 'lib/luoma/parser_unified.rb', line 440 def parse_comment skip_whitespace_control token = eat(:token_comment) carry_whitespace_control eat(:token_comment_end) Comment.new(token, Luoma.get_token_value(token, @source)) end |
#parse_expression(precedence: Precedence::LOWEST) ⇒ Object
203 204 205 206 207 |
# File 'lib/luoma/parser_unified.rb', line 203 def parse_expression(precedence: Precedence::LOWEST) expr = parse_primary(precedence: precedence) expr = parse_ternary(expr) if kind == :token_if expr end |
#parse_false_literal ⇒ Expression
() -> Expression
749 750 751 752 753 754 755 756 757 |
# File 'lib/luoma/parser_unified.rb', line 749 def parse_false_literal token = self.next if PATH_PUNCTUATION.include?(kind) @pos -= 1 parse_path else BooleanLiteral.new(token, false) end end |
#parse_filter(token, left) ⇒ FilteredExpression
(t_token, Expression) -> FilteredExpression
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'lib/luoma/parser_unified.rb', line 783 def parse_filter(token, left) name = parse_path if TERMINATE_FILTER.include?(kind) || kind == :token_comma # No arguments return FilteredExpression.new( token, left, Filter.new( name.token, name, [], [] ) ) end eat(:token_colon, message: "missing colon or pipe") args = [] #: Array[Expression] kwargs = [] #: Array[KeywordArgument] loop do kind_ = kind break if TERMINATE_FILTER.include?(kind_) if kind_ == :token_ident peek_kind = peek.first if KEYWORD_ARGUMENT_DELIMITERS.include?(peek_kind) # A keyword argument param = parse_ident eat_one_of(KEYWORD_ARGUMENT_DELIMITERS) value = parse_expression(precedence: Precedence::FILTER_ARG) kwargs << KeywordArgument.new(param.token, param, value) elsif peek_kind == :token_arrow # A positional argument that is an arrow function with a single # parameter. args << parse_lambda(precedence: Precedence::FILTER_ARG) else # A positional argument that is a variable or path args << parse_expression(precedence: Precedence::FILTER_ARG) end else break if TERMINATE_FILTER.include?(kind_) args << parse_expression(precedence: Precedence::FILTER_ARG) end break if TERMINATE_FILTER.include?(kind) eat(:token_comma, message: "missing comma or pipe") end FilteredExpression.new( token, left, Filter.new( name.token, name, args, kwargs ) ) end |
#parse_float_literal ⇒ Expression
() -> Expression
777 778 779 780 |
# File 'lib/luoma/parser_unified.rb', line 777 def parse_float_literal token = self.next FloatLiteral.new(token, Float(Luoma.get_token_value(token, @source))) end |
#parse_ident ⇒ Name
() -> Name
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/luoma/parser_unified.rb', line 210 def parse_ident token = eat(:token_ident) if PATH_PUNCTUATION.include?(kind) raise TemplateSyntaxError.new( "expected an identifier, found a path", token, @source, @template_name ) end Name.new(token, Luoma.get_token_value(token, @source)) end |
#parse_infix(left) ⇒ Expression
(Expression) -> Expression
856 857 858 859 860 861 862 863 864 |
# File 'lib/luoma/parser_unified.rb', line 856 def parse_infix(left) op_token = self.next kind_ = op_token.first return parse_filter(op_token, left) if kind_ == :token_pipe right = parse_expression(precedence: PRECEDENCES[kind_] || Precedence::LOWEST) INFIX_OPERATORS[kind_].new(op_token, left, right) end |
#parse_int_literal ⇒ Expression
() -> Expression
771 772 773 774 |
# File 'lib/luoma/parser_unified.rb', line 771 def parse_int_literal token = self.next IntegerLiteral.new(token, Luoma.get_token_value(token, @source).to_i) end |
#parse_keyword_arguments(require_commas: nil) ⇒ Array[KeywordArgument]
(?require_commas: bool?) -> Array
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/luoma/parser_unified.rb', line 226 def parse_keyword_arguments(require_commas: nil) args = [] #: Array[KeywordArgument] loop do kind_ = kind break if TERMINATE_EXPRESSION.include?(kind_) name = parse_ident eat_one_of(KEYWORD_ARGUMENT_DELIMITERS) args << KeywordArgument.new(name.token, name, parse_expression) kind_ = kind if require_commas && !TERMINATE_EXPRESSION.include?(kind_) eat(:token_comma) elsif kind_ == :token_comma @pos += 1 end end args end |
#parse_lambda(precedence:) ⇒ Lambda
(precedence: Integer) -> Lambda
849 850 851 852 853 |
# File 'lib/luoma/parser_unified.rb', line 849 def parse_lambda(precedence:) param = parse_ident eat(:token_arrow) Lambda.new(param.token, [param], parse_expression(precedence: precedence)) end |
#parse_lambda_range_or_group(precedence:) ⇒ Expression
(precedence: Integer) -> Expression
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/luoma/parser_unified.rb', line 645 def parse_lambda_range_or_group(precedence:) token = eat(:token_lparen) expr = parse_expression case kind when :token_double_dot # A range literal @pos += 1 stop = parse_expression eat(:token_rparen, message: "expected a closing bracket") RangeLiteral.new(token, expr, stop) when :token_triple_dot raise TemplateSyntaxError.new( "too many dots", current, @source, @template_name ) when :token_comma # A lambda expression, but we've already consumed the first parameter. parse_partial_lambda(expr, precedence: precedence) else if peek.first == :token_arrow && kind == :token_rparen # A lambda expression with a single parameter surrounded by parens. parse_partial_lambda(expr, precedence: precedence) else closing_token = eat(:token_rparen, message: "unbalanced brackets") segments = PATH_PUNCTUATION.include?(kind) ? parse_path_segments : [] #: Array[t_path_segment] GroupExpression.new(token, expr, segments).with(closing_token) end end end |
#parse_name ⇒ Name
Parse an identifier, possibly surrounded by quotes. () -> Name
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/luoma/parser_unified.rb', line 250 def parse_name case kind when :token_ident parse_ident when :token_single_quote, :token_double_quote expr = parse_string_literal if expr.segments.length > 1 || !expr.segments.first.is_a?(String) raise TemplateSyntaxError.new( "expected a string or identifier", current, @source, @template_name ) end Name.new(expr.token, expr.segments[0]) # steep:ignore else raise TemplateSyntaxError.new( "expected a string or identifier", current, @source, @template_name ) end end |
#parse_null_literal ⇒ Expression
() -> Expression
760 761 762 763 764 765 766 767 768 |
# File 'lib/luoma/parser_unified.rb', line 760 def parse_null_literal token = self.next if PATH_PUNCTUATION.include?(kind) @pos -= 1 parse_path else NullLiteral.new(token) end end |
#parse_object_item ⇒ Item, Spread
() -> Item | Spread
635 636 637 638 639 640 641 642 |
# File 'lib/luoma/parser_unified.rb', line 635 def parse_object_item return Spread.new(self.next, parse_expression) if kind == :token_triple_dot key = parse_name eat(:token_colon) value = parse_expression Item.new(key.token, key, value) end |
#parse_object_literal ⇒ ObjectLiteral
() -> ObjectLiteral
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/luoma/parser_unified.rb', line 614 def parse_object_literal token = eat(:token_lbrace) return ObjectLiteral.new(token, []).with(eat(:token_rbrace)) if kind == :token_rbrace items = [parse_object_item] #: Array[Item|Spread] loop do break if kind == :token_rbrace eat(:token_comma) break if kind == :token_rbrace # Trailing commas are OK. items << parse_object_item end eat(:token_rbrace) ObjectLiteral.new(token, items) end |
#parse_output ⇒ Markup
() -> Markup
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/luoma/parser_unified.rb', line 420 def parse_output token = @tokens[@pos - 1] skip_whitespace_control expr = parse_expression carry_whitespace_control unless kind == :token_out_end raise TemplateSyntaxError.new( "bad expression or missing markup delimiter", expr.span, @source, @template_name ) end eat(:token_out_end) OutputStatement.new(token, expr) end |
#parse_partial_array(token, first) ⇒ Expression
(t_token, Expression|Spread) -> Expression
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/luoma/parser_unified.rb', line 590 def parse_partial_array(token, first) items = [first] #: Array[Expression|Spread] loop do break if kind == :token_rbracket eat(:token_comma) case kind when :token_rbracket # Trailing commas are OK. break when :token_triple_dot items << Spread.new(self.next, parse_expression) else items << parse_expression end end eat(:token_rbracket) ArrayLiteral.new(token, items) end |
#parse_partial_lambda(expr, precedence:) ⇒ Expression
(Expression, precedence: Integer) -> Lambda
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/luoma/parser_unified.rb', line 681 def parse_partial_lambda(expr, precedence:) unless expr.is_a?(Variable) && expr.segments.empty? && expr.root.is_a?(Name) raise TemplateSyntaxError.new( "expected an identifier", expr.token, @source, @template_name ) end params = [expr.root] #: Array[Name] @pos += 1 if kind == :token_comma loop do break if kind == :token_rparen params << parse_ident @pos += 1 if kind == :token_comma end eat(:token_rparen) eat(:token_arrow) Lambda.new(expr.token, params, parse_expression(precedence: precedence)) end |
#parse_path ⇒ Variable
() -> Variable
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/luoma/parser_unified.rb', line 468 def parse_path token = current root = if PATH_SEGMENT_KINDS.include?(token.first) @pos += 1 Name.new(token, Luoma.get_token_value(token, @source)) else eat(:token_lbracket) if kind == :token_ident path = parse_path eat(:token_rbracket) path else parse_string_literal.with(eat(:token_rbracket)) end end Variable.new(token, root, parse_path_segments) end |
#parse_path_segments ⇒ Array[t_path_segment]
() -> Array
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/luoma/parser_unified.rb', line 489 def parse_path_segments segments = [] #: Array[t_path_segment] loop do case kind when :token_lbracket segments << parse_bracketed_segment when :token_dot @pos += 1 token = eat_one_of(PATH_SEGMENT_KINDS) if kind == :token_question @pos += 1 segments << Predicate.new( token, Luoma.get_token_value(token, @source) ) if PATH_PUNCTUATION.include?(kind) raise TemplateSyntaxError.new( "unexpected segment after predicate", current, @source, @template_name ) end break end segments << Name.new(token, Luoma.get_token_value(token, @source)) else break end end segments end |
#parse_positional_arguments(require_commas: nil) ⇒ Array[Expression]
(?require_commas: bool?) -> Array
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/luoma/parser_unified.rb', line 278 def parse_positional_arguments(require_commas: nil) args = [] #: Array[Expression] loop do break if TERMINATE_EXPRESSION.include?(kind) args << parse_expression kind_ = kind if require_commas && !TERMINATE_EXPRESSION.include?(kind_) eat(:token_comma) elsif kind_ == :token_comma @pos += 1 end end args end |
#parse_prefix ⇒ Expression
() -> Expression
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/luoma/parser_unified.rb', line 707 def parse_prefix token = self.next case token.first when :token_not NotExpression.new(token, parse_expression(precedence: Precedence::LOGICAL_NOT)) when :token_add PosExpression.new(token, parse_expression(precedence: Precedence::NEG)) when :token_sub NegExpression.new(token, parse_expression(precedence: Precedence::NEG)) else raise TemplateSyntaxError.new( "unknown prefix operator", token, @source, @template_name ) end end |
#parse_primary(precedence: Precedence::LOWEST) ⇒ Expression
(?precedence: Integer) -> Expression
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/luoma/parser_unified.rb', line 342 def parse_primary(precedence: Precedence::LOWEST) left = case kind when :token_single_quote, :token_double_quote parse_string_literal when :token_ident peek.first == :token_arrow ? parse_lambda(precedence: precedence) : parse_path when :token_lbracket parse_array_or_path when :token_lbrace parse_object_literal when :token_lparen parse_lambda_range_or_group(precedence: precedence) when :token_not PATH_PUNCTUATION.include?(peek.first) ? parse_path : parse_prefix when :token_add, :token_sub parse_prefix when :token_true PATH_PUNCTUATION.include?(peek.first) ? parse_path : parse_true_literal when :token_false PATH_PUNCTUATION.include?(peek.first) ? parse_path : parse_false_literal when :token_null, :token_nil PATH_PUNCTUATION.include?(peek.first) ? parse_path : parse_null_literal when :token_int parse_int_literal when :token_float parse_float_literal when :token_and, :token_or, :token_orElse, :token_contains, :token_in, :token_if, :token_else if PATH_PUNCTUATION.include?(peek.first) || TERMINATE_EXPRESSION.include?(peek.first) parse_path else token = current raise TemplateSyntaxError.new( "unexpected operator #{Luoma.get_token_value(token, @source).inspect}", token, @source, @template_name ) end when :token_out_end, :token_wc, :token_tag_end raise TemplateSyntaxError.new( "unexpected empty expression", current, @source, @template_name ) else token = current raise TemplateSyntaxError.new( "expected an expression, found #{Luoma::TOKEN_KIND_MAP[token.first].inspect}", token, @source, @template_name ) end loop do kind_ = kind break if (PRECEDENCES[kind_] || Precedence::LOWEST) < precedence || !INFIX_OPERATORS.include?(kind_) left = parse_infix(left) end left end |
#parse_range_literal ⇒ Expression
() -> Expression
728 729 730 731 732 733 734 735 |
# File 'lib/luoma/parser_unified.rb', line 728 def parse_range_literal token = eat(:token_lparen) start = parse_expression eat(:token_double_dot) stop = parse_expression eat(:token_rparen) RangeLiteral.new(token, start, stop) end |
#parse_string_literal ⇒ StringLiteral
() -> StringLiteral
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/luoma/parser_unified.rb', line 298 def parse_string_literal token = self.next # quote segments = [] #: Array[String|Expression] loop do case kind when :token_single_quoted, :token_double_quoted segments << Luoma.get_token_value(self.next, @source) when :token_single_escaped, :token_double_escaped segments << unescape(self.next) when :token_interpolation_start start_token = self.next segments << parse_expression unless kind == :token_interpolation_end raise TemplateSyntaxError.new( "unclosed template string expression", Luoma.span(start_token, current), @source, @template_name ) end eat(:token_interpolation_end) else break end end unless kind == token.first raise TemplateSyntaxError.new( "unclosed string literal", Luoma.span(token, @tokens[@pos - 1] || current), @source, @template_name ) end StringLiteral.new(token, segments, eat(token.first)) end |
#parse_tag ⇒ Markup
() -> Markup
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/luoma/parser_unified.rb', line 449 def parse_tag skip_whitespace_control token = eat(:token_tag_name, message: "missing tag name") tag_name = Luoma.get_token_value(token, @source) tag = @env.[tag_name] unless tag raise TemplateSyntaxError.new( "unexpected tag #{Luoma.get_token_value(token, @source).inspect}", token, @source, @template_name ) end tag.parse(token, tag_name, self) end |
#parse_ternary(consequence) ⇒ Expression
(Expression) -> Expression
408 409 410 411 412 413 414 415 416 417 |
# File 'lib/luoma/parser_unified.rb', line 408 def parse_ternary(consequence) eat(:token_if) condition = parse_primary alternative = if kind == :token_else @pos += 1 parse_primary end TernaryExpression.new(consequence.token, consequence, condition, alternative) end |
#parse_true_literal ⇒ Expression
() -> Expression
738 739 740 741 742 743 744 745 746 |
# File 'lib/luoma/parser_unified.rb', line 738 def parse_true_literal token = self.next if PATH_PUNCTUATION.include?(kind) @pos -= 1 parse_path else BooleanLiteral.new(token, true) end end |
#unescape(token) ⇒ String
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 |
# File 'lib/luoma/parser_unified.rb', line 867 def unescape(token) unescaped = [] # : Array[String] scanner = StringScanner.new(Luoma.get_token_value(token, @source)) until scanner.eos? if scanner.scan(RE_SLASH_U) code_point = (scanner.captures&.first || raise).to_i(16) if low_surrogate?(code_point) raise TemplateSyntaxError.new( "unexpected low surrogate", token, @source, @template_name ) end if high_surrogate?(code_point) unless scanner.scan(RE_SLASH_U) raise TemplateSyntaxError.new( "expected low surrogate", token, @source, @template_name ) end low_surrogate = (scanner.captures&.first || raise).to_i(16) unless low_surrogate?(low_surrogate) raise TemplateSyntaxError.new( "expected low surrogate", token, @source, @template_name ) end code_point = 0x10000 + ( ((code_point & 0x03FF) << 10) | (low_surrogate & 0x03FF) ) end unescaped << code_point.chr(Encoding::UTF_8) next end if scanner.scan(RE_ESCAPE_INTERPOLATION) unescaped << "${" next end ch = scanner.getch break if ch.nil? unless ch == "\\" unescaped << ch next end ch = scanner.getch case ch when "\"" unescaped << "\"" when "'" unescaped << "'" when "\\" unescaped << "\\" when "/" unescaped << "/" when "b" unescaped << "\x08" when "f" unescaped << "\x0c" when "n" unescaped << "\n" when "r" unescaped << "\r" when "t" unescaped << "\t" when nil raise TemplateSyntaxError.new( "incomplete escape sequence", token, @source, @template_name ) else raise TemplateSyntaxError.new( "unknown escape sequence", token, @source, @template_name ) end end unescaped.join end |