lib/liquidscript/scanner/liquidscript.rb in liquidscript-0.4.1 vs lib/liquidscript/scanner/liquidscript.rb in liquidscript-0.5.0

- old
+ new

@@ -28,20 +28,27 @@ -- ~ new return typeof + throw ) + set :actions, %w( + break + continue + ) + set :binops, %w( - + - * / & | ^ + + - * / ^ << >> >>> == === != !== > >= < <= && || + & | instanceof or and ) set :keywords, %w( @@ -57,64 +64,135 @@ on("module") { emit :module } on("if") { emit :if } on("unless") { emit :unless } on("elsif") { emit :elsif } on("else") { emit :else } + on("for") { emit :for } + on("while") { emit :while } + on("try") { emit :try } + on("catch") { emit :catch } + on("finally") { emit :finally } on(:number) { |m| emit :number, m } on(:string) { |m| emit :sstring, m } on(:keywords) { |m| emit :keyword, m } - on(:unops) { |m| emit :unop, m } + on(:actions) { |m| emit :action, m } on("->") { emit :arrow } on("=") { emit :equal } - on("{") { emit :lbrack } + on("{") { emit :lbrace } on("(") { emit :lparen } - on("[") { emit :lbrace } - on("}") { emit :rbrack } + on("[") { emit :lbrack } + on("}") { emit :rbrace } on(")") { emit :rparen } - on("]") { emit :rbrace } + on("]") { emit :rbrack } on(":") { emit :colon } on(".") { emit :prop } on(",") { emit :comma } - on("\n") { line!; emit :newline} + on("\n") { line! } + on(%r{"} => :istring) + on(%r{<<([A-Z]+)}) do |_, s| + emit :heredoc_ref, s + @lexes << [:heredoc, s] + end + on(%r{<<-([A-Z]+)}) do |_, s| + emit :iheredoc_ref, s + @lexes << [:iheredoc, s] + end + on(%r{/(.*?)/([a-z]*)}) { |_, m, b| + emit :regex, [m, b] + } + on("///" => :block_regex) on(:binops) { |m| emit :binop, m } + on(:unops) { |m| emit :unop, m } on(:identifier) { |m| emit :identifier, m } - on(/\"/ => :istring) - on(/#.*?\n/) { } - on(/[\s]/) { } - on(:_) { error } + on(%r{#.*?\n}) { } + on(%r{\s}) { } + on(:_) { |m| error } end context :istring do - init do - @buffer = [] - end + init { @buffer = [] } - on(/\\"/) { |m| @buffer << m } - on(/"/) do + on('\\"') { |m| @buffer << m } + on('"') do emit :istring, @buffer.join exit end - on(/\#\{(.*?)\}/) do |_, b| + on(%r{\#\{(.*?)\}}) do |_, b| emit :istring_begin, @buffer.join lex :main => b @buffer = [] end on(:_) { |m| @buffer << m } end + + context :heredoc do + init { @buffer = [] } + + on(%r{\n\s*([A-Z]+)\s*\n}) do |_, s| + if @start == s + emit :heredoc, @buffer.join + exit + else + @buffer << _ + end + end + + on(:_) { |m| @buffer << m } + end + + context :iheredoc do + init { @buffer = [] } + + on(%r{\n\s*([A-Z]+)\s*\n}) do |_, s| + if @start == s + emit :iheredoc, @buffer.join + @start = nil + exit + else + @buffer << _ + end + end + + on(%r{\#\{(.*?)\}}) do |_, b| + emit :iheredoc_begin, @buffer.join + lex :main => b + @buffer = [] + end + + on(:_) { |m| @buffer << m } + end + + context :block_regex do + init { @buffer = [] } + + on(%r{///([a-z]*)}) do |_, m| + emit :regex, [@buffer.join, m] + exit + end + on(%r{#.*?\n}) { } + on("\n") { } + on(:_) { |m| @buffer << m } + end end def initialize(*) @line = 1 @cstart = 0 + @lexes = [] super end def line! @line += 1 @cstart = @scanner.pos + while @lexes.any? + type, @start = @lexes.shift + + lex type + end end def line @line end