Class: Bijou::Parser
Overview
The Bijou parser is a bi-modal top-down LL(1) parser. It is bi-modal in that it uses two lexers. One for markup and the other for Bijou tags.
Instance Attribute Summary collapse
-
#diagnostics ⇒ Object
readonly
Returns the value of attribute diagnostics.
-
#filename ⇒ Object
readonly
The filename is written to line marker comments in the output.
Instance Method Summary collapse
- #assert(c) ⇒ Object
- #attach_file(file, filename) ⇒ Object
- #debug(str) ⇒ Object
- #debug_(str) ⇒ Object
- #detach_file ⇒ Object
- #diagnostic(m, l, c) ⇒ Object
- #eatArgumentWhitespace ⇒ Object
- #eatLineWhitespace ⇒ Object
- #eatWhitespace ⇒ Object
- #error(s, line = nil, column = nil) ⇒ Object
- #expectChar(ch, msg = '') ⇒ Object
- #findTagClose(openToken, closeToken) ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #message(s, line = nil, column = nil) ⇒ Object
- #parse(component, file, filename = '') ⇒ Object
-
#parse_file(component, file, filename = '') ⇒ Object
The main top-level parsing loop looks for the special Bijou start tags, dispatches to the specialized parsing routines, and manages the lexer switching process.
- #parse_string(component, text) ⇒ Object
- #parseArgsSection ⇒ Object
- #parseCallArgList ⇒ Object
-
#parseCallArgValue ⇒ Object
Parse until ‘,’ or ‘&>’ is found.
-
#parseCallTag(tagStart) ⇒ Object
Parses a tag of the form <& name { arg-list } &> where arg-list is an optional Ruby hash.
- #parseCharSequence(re) ⇒ Object
-
#parseDirectiveTag(tagStart) ⇒ Object
Parses tags of the form <%! name1=“value1” name2 = “value2” …
- #parseFilename ⇒ Object
- #parseIdentifier ⇒ Object
-
#parseInlineTag(tagStart) ⇒ Object
Parses tags of the form <% …
-
#parseNamedTag(tagStart) ⇒ Object
Parses named tags of the form <%name> and </%name>.
-
#parseOutputFlags ⇒ Object
Returns a two item array as a result, the string following the pipe operator ‘|’ and a hash containing filters, if found.
-
#parseOutputTag(tagStart) ⇒ Object
Parses tags of the form <%= …
-
#parseTag(tagStart) ⇒ Object
This routine handles the non-markup tags that are meaningful to the Bijou processor.
- #parseTagName ⇒ Object
- #render(component, source_filename = nil, cache_filename = nil, base = nil, require_list = nil) ⇒ Object
-
#stripLeader(buffer) ⇒ Object
Whitespace formatting helper that removes a single leading blank line when preceded by a tag.
-
#stripTrailer(buffer) ⇒ Object
Whitespace formatting helper that removes any whitespace at the end of a component.
- #warning(s, line = nil, column = nil) ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bijou/parser.rb', line 43 def initialize @debug = false # REVIEW: Make this an option @strip = true @strippingTag = false # @debug = true @diagnostics = nil @input = nil @backend = nil detach_file end |
Instance Attribute Details
#diagnostics ⇒ Object (readonly)
Returns the value of attribute diagnostics.
41 42 43 |
# File 'lib/bijou/parser.rb', line 41 def diagnostics @diagnostics end |
#filename ⇒ Object (readonly)
The filename is written to line marker comments in the output.
39 40 41 |
# File 'lib/bijou/parser.rb', line 39 def filename @filename end |
Instance Method Details
#assert(c) ⇒ Object
125 126 127 128 129 |
# File 'lib/bijou/parser.rb', line 125 def assert(c) if (!c) error("assert failed on line " + @lexer.line.to_s) end end |
#attach_file(file, filename) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bijou/parser.rb', line 73 def attach_file(file, filename) @filename = filename @diagnostics = Diagnostics.new trace = false use_markers = true # The Perl-oriented '#line' marker, for stack traces. @input = LexerInput.new(file, @diagnostics) @backend = Backend.new(@diagnostics, trace, use_markers) @topLexer = TextLexer.new(@input) @tagLexer = TagLexer.new(@input) @lexer = @topLexer @parsingArgs = false @namedSection = nil end |
#debug(str) ⇒ Object
121 122 123 |
# File 'lib/bijou/parser.rb', line 121 def debug(str) puts str if @debug end |
#debug_(str) ⇒ Object
117 118 119 |
# File 'lib/bijou/parser.rb', line 117 def debug_(str) print str if @debug end |
#detach_file ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bijou/parser.rb', line 56 def detach_file @filename = '' # We keep the diagnostics and the backend # @diagnostics # @backend @input = nil @topLexer = nil @tagLexer = nil @lexer = nil @parsingArgs = false @namedSection = nil end |
#diagnostic(m, l, c) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/bijou/parser.rb', line 92 def diagnostic(m, l, c) if !l; l = @lexer.line; end if !c; c = @lexer.column; end m.at(l, c) m end |
#eatArgumentWhitespace ⇒ Object
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 |
# File 'lib/bijou/parser.rb', line 167 def eatArgumentWhitespace() lineComment = false while tok = @lexer.next_token if lineComment if tok == Token::Char && @lexer.text == "\n" # warning("eat arg crlf") lineComment = false next else # Eat comments next end end if tok == Token::Char str = @lexer.text if str == '#' # warning("eat arg comment") lineComment = true elsif str == "\n" # warning("eat arg crlf") # lineComment = false elsif @lexer.text =~ /\s/ # Eat whitespace else # non-whitespace # This should be an identifier # warning("eat arg ws 1 #{@lexer.text}") @lexer.pop_token return true end else # warning("eat arg ws 3 #{@lexer.text} #{tok}") @lexer.pop_token return true end end # EOF return false end |
#eatLineWhitespace ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/bijou/parser.rb', line 157 def eatLineWhitespace() while tok = @lexer.next_token if tok != Token::Char || @lexer.text !~ /\s/ || @lexer.text == '\n' @lexer.pop_token return true end end return nil end |
#eatWhitespace ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/bijou/parser.rb', line 147 def eatWhitespace() while tok = @lexer.next_token if tok != Token::Char || @lexer.text !~ /\s/ @lexer.pop_token return true end end return nil end |
#error(s, line = nil, column = nil) ⇒ Object
111 112 113 114 115 |
# File 'lib/bijou/parser.rb', line 111 def error(s, line=nil, column=nil) m = Error.new m << s @input.diagnostics.add_error(diagnostic(m, line, column)) end |
#expectChar(ch, msg = '') ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/bijou/parser.rb', line 238 def expectChar(ch, msg='') if tok = @lexer.next_token if tok == Token::Char if @lexer.text == '=' return true end end error("expected #{ch}#{msg}") else error("expected #{ch}#{msg} at end of file") end false end |
#findTagClose(openToken, closeToken) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/bijou/parser.rb', line 252 def findTagClose(openToken, closeToken) while tok = @lexer.next_token if tok == Token::TagClose tagClose = @lexer.text if tagClose != closeToken error("expected '#{closeToken}' to close '#{openToken}' tag") end return end end error("expected close tag for '#{openToken}' at end of file") end |
#message(s, line = nil, column = nil) ⇒ Object
99 100 101 102 103 |
# File 'lib/bijou/parser.rb', line 99 def (s, line=nil, column=nil) m = Message.new m << s @input.diagnostics.(diagnostic(m, line, column)) end |
#parse(component, file, filename = '') ⇒ Object
919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/bijou/parser.rb', line 919 def parse(component, file, filename='') if !file.respond_to?(:read) if file.respond_to?(:to_s) # This is a fallback; use StringIO for better performance. file = StringIO.new(file.dup) else raise ArgumentError, "must be a file or a string" end end parse_file(component, file, filename='') render(component) end |
#parse_file(component, file, filename = '') ⇒ Object
The main top-level parsing loop looks for the special Bijou start tags, dispatches to the specialized parsing routines, and manages the lexer switching process.
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 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 |
# File 'lib/bijou/parser.rb', line 943 def parse_file(component, file, filename='') # # Initialize parser members. # attach_file(file, filename) # # Main parsing loop # source_line = @lexer.line buffer = '' while tok = @lexer.next_token if tok == Token::Char buffer << @lexer.text elsif tok == Token::TagOpen if @strip && @strippingTag buffer = stripLeader(buffer) end @strippingTag = false @backend.markup_section(buffer, @filename, source_line) buffer = '' # NOTE: Care must be taked in the implementation of the parsing # routines because the lookahead token may buffer characters from # the input stream. If this causes problems, then the lexer should # should flush (push) the lookahead characters. tagStart = @lexer.text @lexer = @tagLexer parseTag(tagStart) @lexer = @topLexer if @parsingArgs @topLexer.tokenize_arguments = true parseArgsSection @topLexer.tokenize_arguments = false end source_line = @lexer.line else # puts "Token: " + @lexer.text error("Unexpected token #{@lexer.text}") end end if @parsingArgs error("no end tag for args section at end of file") @parsingArgs = false end # Final section if !buffer.empty? # The last blank lines are stripped if @strip buffer = stripTrailer(buffer) end @backend.markup_section(buffer, @filename, source_line) buffer = '' end # print "Chars: " + @input.character.to_s detach_file end |
#parse_string(component, text) ⇒ Object
934 935 936 937 938 |
# File 'lib/bijou/parser.rb', line 934 def parse_string(component, text) file = StringIO.new(text.dup) parse_file(component, file) file.close end |
#parseArgsSection ⇒ Object
770 771 772 773 774 775 776 777 778 779 780 781 782 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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/bijou/parser.rb', line 770 def parseArgsSection # @lexer.start(tagStart, TagType::Call) debug("parseArgsSection") start_line = @lexer.line start_column = @lexer.column loop do lineComment = false # warning("before whitespace") eatArgumentWhitespace # warning("after whitespace") tok = @lexer.next_token if tok == Token::TagOpen # Normal argument end case. @lexer.pop_token return else @lexer.pop_token end # warning('after whitespace') argName = parseIdentifier if argName.empty? error('expected argument identifier') end # warning("after identifier '#{argName}'") # Eat whitespace only up until a linefeed. eatLineWhitespace # NOTE: We require that any value specified be a single-line expression. # Although the backend may be able to render a multi-line assignment, # it is not possible to distinguish between a lone identifier and an # expression continuation. (In addition, our lexer does not tokenize # identifiers, [the parser does], which would preclude a single-token # lookahead. This would only be useful if the assignment operator were # required.) tok = @lexer.next_token if tok == Token::TagOpen # warning('args end 2') # The last argument has no value. @backend.add_argument(argName, nil, @filename, @lexer.line, @lexer.column) @lexer.pop_token return elsif tok == Token::Operator && @lexer.text == '=>' # warning('=>') argValue = '' while tok = @lexer.next_token if tok == Token::Char str = @lexer.text if str == '#' || str == "\n" @lexer.pop_token argValue = argValue.strip if argValue.empty? error('An argument assignment has no value') else # warning "comment/eol #{argName} => #{argValue}" @backend.add_argument(argName, argValue, @filename, @lexer.line, @lexer.column) end argName = nil argValue = '' break else argValue << str end elsif tok == Token::String argValue << @lexer.text elsif tok == Token::TagOpen # Syntax error of the form: # arg-name => </%args> error('An argument assignment has no value') @lexer.pop_token return end end elsif tok == Token::Char && (@lexer.text == '#' || @lexer.text =~ /\w/) # warning("comment or new identifier") @backend.add_argument(argName, nil, @filename, @lexer.line, @lexer.column) @lexer.pop_token else error("unexpected token '#{@lexer.text}' after argument '#{argName}'") @lexer.pop_token # Wait for the end token while tok = @lexer.next_token if tok == Token::TagOpen @lexer.pop_token return end end return end end # while true end |
#parseCallArgList ⇒ Object
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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/bijou/parser.rb', line 536 def parseCallArgList debug("parseCallArgList") start_line = @lexer.line start_column = @lexer.column arg_list = [] arg_name = nil arg_value = nil loop do eatWhitespace arg_name = parseFilename if !arg_name || arg_name.empty? error("Argument name expected") return nil end eatWhitespace if tok = @lexer.next_token if tok == Token::Operator && @lexer.text == '=>' arg_value = parseCallArgValue debug "arg_value #{arg_value}" if arg_value && !arg_value.empty? # if arg_list.has_key?(arg_name) # warning("argument '#{arg_name}' duplicated") # end arg_list.push [arg_name, arg_value] arg_name = nil arg_value = nil else error("empty argument value after assignment operator '=>'") return nil end # look for , or %> else error("expected assignment operator '=>' after argument identifier #{@lexer.text}") @lexer.pop_token return nil end else error("unclosed call tag") return nil end eatWhitespace if tok = @lexer.next_token if tok == Token::TagClose tagClose = @lexer.text if tagClose == '&>' # End of successful parse @lexer.pop_token return arg_list else error("expected '&>' to close call tag at (#{start_line}, #{start_column})") @lexer.pop_token return nil end elsif tok != Token::Char || @lexer.text != ',' error("unexpected token '#{@lexer.text}' after argument value", start_line, start_column) @lexer.pop_token # End of argument list return nil end end end end |
#parseCallArgValue ⇒ Object
Parse until ‘,’ or ‘&>’ is found.
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 527 528 529 530 531 532 533 534 |
# File 'lib/bijou/parser.rb', line 500 def parseCallArgValue result = '' start_line = @lexer.line start_column = @lexer.column while tok = @lexer.next_token if tok == Token::Char if @lexer.text == ',' @lexer.pop_token return result.strip end result << @lexer.text elsif tok == Token::String result << @lexer.text elsif tok == Token::Operator # We have to include this in case it is within another hash. result << @lexer.text elsif tok == Token::TagClose tagClose = @lexer.text if tagClose != '&>' error("expected '&>' to close call tag at (#{start_line}, #{start_column})") end @lexer.pop_token return result.strip else error("unexpected token '#{@lexer.text}'", start_line, start_column) return nil end end error("unclosed call tag after assignment '=>'") return nil end |
#parseCallTag(tagStart) ⇒ Object
Parses a tag of the form <& name { arg-list } &> where arg-list is an optional Ruby hash.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/bijou/parser.rb', line 609 def parseCallTag(tagStart) @lexer.start(tagStart, TagType::Call) debug("parseCallTag #{tagStart}") start_line = @lexer.line start_column = @lexer.column # Is this an indirect call? indirect = false if tok = @lexer.next_token if tok == Token::Char && @lexer.text == '=' indirect = true else @lexer.pop_token end end # REVIEW: Should we allow indirect calls with @member syntax? eatWhitespace callIdentifier = parseFilename if !callIdentifier || callIdentifier.empty? error("Identifier expected in call tag") end arg_list = nil eatWhitespace if tok = @lexer.next_token if tok == Token::Char if @lexer.text == ',' @lexer.tokenize_arguments = true arg_list = parseCallArgList @lexer.tokenize_arguments = false findTagClose('<&', '&>') if arg_list # Send to the backend @backend.call_tag(callIdentifier, arg_list, indirect, @filename, start_line) end return else error("unexpected token '#{@lexer.text}' after call identifier", start_line, start_column) findTagClose('<&', '&>') return end else tagClose = @lexer.text if tagClose != '&>' error("expected '&>' to close call tag at (#{start_line}, #{start_column})") end result = {} # Send to the backend @backend.call_tag(callIdentifier, result, indirect, @filename, start_line) return end end error("unclosed call tag") return while tok = @lexer.next_token if tok == Token::Char result << @lexer.text elsif tok == Token::String result << @lexer.text elsif tok == Token::TagClose tagClose = @lexer.text # BUGBUG <& X %> doesn't fire. if tagClose != '&>' error("expected '&>' to close call tag at (#{start_line}, #{start_column})") end if result.strip.empty? result = '{}' end # Send to the backend @backend.call_tag(callIdentifier, result, indirect, @filename, start_line) return else error("unexpected tag token '#{@lexer.text}'", start_line, start_column) end end error("unclosed call tag") end |
#parseCharSequence(re) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/bijou/parser.rb', line 211 def parseCharSequence(re) debug_("parseCharSequence: ") name = '' while tok = @lexer.next_token text = @lexer.text if tok == Token::Char && text =~ re name << text else @lexer.pop_token debug(name) return name end end debug(name + "<eof>") return name end |
#parseDirectiveTag(tagStart) ⇒ Object
Parses tags of the form <%! name1=“value1” name2 = “value2” … %>, which control compile time and runtime behavior. These tags are usually placed near the top of the file.
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/bijou/parser.rb', line 710 def parseDirectiveTag(tagStart) @lexer.start(tagStart, TagType::Directive) start_line = @lexer.line start_column = @lexer.column here = 'in <%! directive' errors = 0 directives = {} while true eatWhitespace if tok = @lexer.next_token if tok == Token::TagClose if @lexer.text == '%>' if directives.length > 0 # Send to the backend @backend.directive_tag(directives, start_line, start_column) # Whitespace following this tag may be stripped @strippingTag = true end else error("expected '%>' to close directive '<%!' tag") end return # Main loop exit else @lexer.pop_token end end identifierName = parseIdentifier if identifierName.empty? error("expected identifier in directive") end eatWhitespace if !expectChar('=', " after literal #{here}") findTagClose('<%!', '%>') return end eatWhitespace if tok = @lexer.next_token if tok == Token::String directives[identifierName] = @lexer.text else error("unexpected token #{@lexer.text} after assignment #{here}") findTagClose('<%!', '%>') return end end end end |
#parseFilename ⇒ Object
234 235 236 |
# File 'lib/bijou/parser.rb', line 234 def parseFilename parseCharSequence(/[\\\/\w\.]/) end |
#parseIdentifier ⇒ Object
230 231 232 |
# File 'lib/bijou/parser.rb', line 230 def parseIdentifier parseCharSequence(/\w/) end |
#parseInlineTag(tagStart) ⇒ Object
Parses tags of the form <% … %>, which may contain any valid Ruby statements. The contents of the inline tag are evaluated at runtime during the render phase. The puts and print methods may be used to render to the output stream.
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/bijou/parser.rb', line 408 def parseInlineTag(tagStart) @lexer.start(tagStart, TagType::Inline) debug("parseInlineTag #{tagStart}") # BUGBUG: Turn off quote handling in comments. # BUGBUG: We may want to turn of string parsing here (at the cost of not # handling quoted close tokens). start_line = @lexer.line start_column = @lexer.column result = '' while tok = @lexer.next_token if tok == Token::Char result << @lexer.text elsif tok == Token::String result << @lexer.text elsif tok == Token::TagClose tagClose = @lexer.text if tagClose != '%>' error("expected '%>' to close inline '<%' tag at (#{start_line}, #{start_column})") end debug("#{result}%>") # Send to the backend @backend.inline_tag(result, @filename, start_line) return else error("unexpected tag token '#{@lexer.text}'", start_line, start_column) end end if @lexer.line != start_line error("unclosed inline tag (was this supposed to be a named tag?)", start_line, start_column) else error("unclosed inline tag (was this supposed to be a named tag?)") end end |
#parseNamedTag(tagStart) ⇒ Object
Parses named tags of the form <%name> and </%name>. Due to the ambiguity that the start token causes between inline tags and named tags, this routine performs a lookahead and dispatches to the correct parsing routine.
294 295 296 297 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 338 339 340 341 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 |
# File 'lib/bijou/parser.rb', line 294 def parseNamedTag(tagStart) debug("parseNamedTag #{tagStart}") nameIdentifier = nil if tagStart == '<%' tagName = parseTagName case tagName when 'init' @namedSection = tagName when 'fini' @namedSection = tagName when 'method' @namedSection = tagName when 'args' if @parsingArgs error("args may not be nested within other args sections") end @parsingArgs = true else @lexer.start(tagStart, TagType::Inline) parseInlineTag(tagStart) return end @lexer.start(tagStart, TagType::Named) start_line = @lexer.line start_column = @lexer.column if tagName == 'method' # <%method name... # ^ debug_("ws(") eatWhitespace debug(")") nameIdentifier = parseIdentifier if nameIdentifier.empty? error("expected method name") end end else assert(tagStart == '</%') @lexer.start(tagStart, TagType::Named) start_line = @lexer.line start_column = @lexer.column # Whitespace following this tag may be stripped @strippingTag = true tagName = parseTagName if tagName case tagName when 'init' when 'fini' when 'args' @parsingArgs = false when 'method' else error("section close type '</%#{tagName}' is unrecognized") end @namedSection = nil else error("expected name after '</%'") end end # <%name ... # ^ debug_("eatws(") eatWhitespace debug(")") # # NOTE: The token may be a Char, even though it's for a tag close. # # This is a special case for when a tag name goes against the close # tag token (e.g., <%init>). Because of the ambiguity caused by the two # different tag types opening with the same token, we must lookahead while # we parse the identifier. Since we haven't switched into TagType::Named # yet, '>' is regarded as a Char instead of a TagClose. # tok = @lexer.next_token tagClose = @lexer.text if tok == Token::TagClose || tok == Token::Char if tagClose == '>' # Send to the backend if tagStart == '<%' @backend.named_start_tag(tagName, nameIdentifier, start_line, start_column) else assert(tagStart == '</%') @backend.named_end_tag(tagName) end else error("expected '>' to close #{tagName} tag") end else error("unexpected token '#{@lexer.text}' in " + "#{tagName} tag", start_line, start_column) end end |
#parseOutputFlags ⇒ Object
Returns a two item array as a result, the string following the pipe operator ‘|’ and a hash containing filters, if found.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/bijou/parser.rb', line 267 def parseOutputFlags result = '' filters = [] while tok = @lexer.next_token if tok == Token::Char if @lexer.text =~ /[a-z]/ filters.push @lexer.text elsif @lexer.text !~ /\s/ @lexer.pop_token return [result, []] end result << @lexer.text elsif tok == Token::TagClose @lexer.pop_token return [result, filters] else @lexer.pop_token return [result, []] end end error("expected '%>' to close output tag '<%=' at end of file") return [result, []] end |
#parseOutputTag(tagStart) ⇒ Object
Parses tags of the form <%= … %> where the contents may be any valid Ruby expression. This expression is evaluated at runtime in a string context and the results are rendered to the output stream.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/bijou/parser.rb', line 453 def parseOutputTag(tagStart) @lexer.start(tagStart, TagType::Output) debug("parseOutputTag #{tagStart}") start_line = @lexer.line start_column = @lexer.column result = '' filters = nil while tok = @lexer.next_token if tok == Token::Char if @lexer.text == '|' # Returns an array containing [string, array] flags = parseOutputFlags if flags[1].length > 0 # Flags were found. filters = flags[1] else # False alarm result << '|' + flags[0] end else result << @lexer.text end elsif tok == Token::String result << @lexer.text elsif tok == Token::TagClose tagClose = @lexer.text if tagClose != '%>' error("expected '%>' to close output '<%=' tag") end # Send to the backend @backend.output_tag(result, filters, @filename, start_line) return else error("invalid output tag syntax", start_line, start_column) error("unexpected token '#{@lexer.text}'") end end error("unclosed output tag") end |
#parseTag(tagStart) ⇒ Object
This routine handles the non-markup tags that are meaningful to the Bijou processor. It dispatches to the specific tag handlers. Parsing is done with the specialized tag lexer. Normal markup is parsed using a lexer that only recognizes Bijou tag open tokens. This bi-modal system allows us to ignore the complex parsing that would be required to handle HTML, scripts, style sheets, etc.
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 |
# File 'lib/bijou/parser.rb', line 885 def parseTag(tagStart) tagName = nil @backend.tag_open(tagStart) case tagStart when '<%'; # This handler will determine whether it's named or inline. parseNamedTag(tagStart) return when '<%=' parseOutputTag(tagStart) return when '<&'; parseCallTag(tagStart) return when '</%'; # This handler will determine whether it's named or inline. parseNamedTag(tagStart) return when '<%!' parseDirectiveTag(tagStart) return else; raise "unexpected tag type #{tagStart}" end end |
#parseTagName ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/bijou/parser.rb', line 131 def parseTagName debug_("parseTagName: ") # If the first character isn't a word character, return nil tag = '' while tok = @lexer.next_token if tok != Token::Char || /\w/ !~ @lexer.text @lexer.pop_token debug(tag) return tag end tag << @lexer.text end debug(tag + "<eof>") end |
#render(component, source_filename = nil, cache_filename = nil, base = nil, require_list = nil) ⇒ Object
913 914 915 916 917 |
# File 'lib/bijou/parser.rb', line 913 def render(component, source_filename=nil, cache_filename=nil, base=nil, require_list=nil) @backend.render(component, source_filename, cache_filename, base, require_list) end |
#stripLeader(buffer) ⇒ Object
Whitespace formatting helper that removes a single leading blank line when preceded by a tag.
1011 1012 1013 1014 1015 1016 |
# File 'lib/bijou/parser.rb', line 1011 def stripLeader(buffer) if buffer =~ /\A\s*?\n(.*)\Z/m return $1 end return buffer end |
#stripTrailer(buffer) ⇒ Object
Whitespace formatting helper that removes any whitespace at the end of a component.
1020 1021 1022 1023 1024 1025 |
# File 'lib/bijou/parser.rb', line 1020 def stripTrailer(buffer) if buffer =~ /\A(.*?)\s*\Z/m return $1 end return buffer end |