diff --git a/Rakefile b/Rakefile index e47bda3..df4cc88 100644 --- a/Rakefile +++ b/Rakefile @@ -8,7 +8,7 @@ GEMSPEC = Gem::Specification.load('oga.gemspec') LEXER_INPUT = 'lib/oga/lexer.rl' LEXER_OUTPUT = 'lib/oga/lexer.rb' -HTML_PARSER = 'lib/oga/parser/html.rb' +HTML_PARSER = 'lib/oga/parser.rb' GENERATED_FILES = ['coverage', 'yardoc', LEXER_OUTPUT, HTML_PARSER] diff --git a/lib/oga.rb b/lib/oga.rb index 997e7fa..4952316 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -2,4 +2,4 @@ require 'ast' require_relative 'oga/ast/node' require_relative 'oga/lexer' -require_relative 'oga/parser/html' +require_relative 'oga/parser' diff --git a/lib/oga/parser.rb b/lib/oga/parser.rb new file mode 100644 index 0000000..531a395 --- /dev/null +++ b/lib/oga/parser.rb @@ -0,0 +1,254 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.4.11 +# from Racc grammer file "". +# + +require 'racc/parser.rb' +module Oga + class Parser < Racc::Parser + + + def initialize + @lexer = Lexer.new + end + + def reset + @lines = [] + @line = 1 + @column = 1 + end + + def s(type, *children) + return AST::Node.new( + type, + children.flatten, + :line => @line, + :column => @column + ) + end + + def next_token + type, value, line, column = @tokens.shift + + @line = line if line + @column = column if column + + return type ? [type, value] : [false, false] + end + + def on_error(type, value, stack) + name = token_to_str(type) + line_str = @lines[@line - 1] + indicator = '~' * (@column - 1) + '^' + + raise Racc::ParseError, <<-EOF.strip +Failed to parse the supplied input. + +Reason: unexpected #{name} with value #{value.inspect} +Location: line #{@line}, column #{@column} + +Offending code: + +#{line_str} +#{indicator} + +Current stack: + +#{stack.inspect} + EOF + end + + def parse(string) + @lines = string.lines + @tokens = @lexer.lex(string) + ast = do_parse + + reset + + return ast + end + +# vim: set ft=racc: +##### State transition tables begin ### + +racc_action_table = [ + 7, 7, 14, 11, 13, 9, 15, 16, 18, 19, + 20, nil, 6, 6 ] + +racc_action_check = [ + 0, 2, 9, 7, 8, 1, 11, 12, 16, 18, + 19, nil, 0, 2 ] + +racc_action_pointer = [ + -4, 5, -3, nil, nil, nil, nil, -12, -11, 2, + nil, 1, 3, nil, nil, nil, 2, nil, -6, 5, + nil ] + +racc_action_default = [ + -2, -16, -1, -4, -5, -6, -7, -16, -16, -16, + -3, -16, -16, -11, 21, -8, -16, -10, -16, -16, + -9 ] + +racc_goto_table = [ + 3, 1, 10, 2, 17, 12 ] + +racc_goto_check = [ + 3, 1, 3, 2, 7, 8 ] + +racc_goto_pointer = [ + nil, 1, 3, 0, nil, nil, nil, -8, -3, nil, + nil ] + +racc_goto_default = [ + nil, nil, nil, nil, 4, 5, 8, nil, nil, nil, + nil ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 1, 18, :_reduce_1, + 0, 18, :_reduce_2, + 2, 19, :_reduce_3, + 1, 19, :_reduce_4, + 1, 20, :_reduce_none, + 1, 20, :_reduce_none, + 1, 22, :_reduce_7, + 3, 23, :_reduce_8, + 4, 24, :_reduce_none, + 3, 21, :_reduce_10, + 1, 25, :_reduce_none, + 2, 26, :_reduce_none, + 1, 26, :_reduce_none, + 1, 27, :_reduce_none, + 1, 27, :_reduce_none ] + +racc_reduce_n = 16 + +racc_shift_n = 21 + +racc_token_table = { + false => 0, + :error => 1, + :T_SPACE => 2, + :T_NEWLINE => 3, + :T_SMALLER => 4, + :T_GREATER => 5, + :T_SLASH => 6, + :T_DQUOTE => 7, + :T_SQUOTE => 8, + :T_DASH => 9, + :T_RBRACKET => 10, + :T_LBRACKET => 11, + :T_COLON => 12, + :T_BANG => 13, + :T_EQUALS => 14, + :T_TEXT => 15, + :T_DOCTYPE => 16 } + +racc_nt_base = 17 + +racc_use_result_var = false + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] + +Racc_token_to_s_table = [ + "$end", + "error", + "T_SPACE", + "T_NEWLINE", + "T_SMALLER", + "T_GREATER", + "T_SLASH", + "T_DQUOTE", + "T_SQUOTE", + "T_DASH", + "T_RBRACKET", + "T_LBRACKET", + "T_COLON", + "T_BANG", + "T_EQUALS", + "T_TEXT", + "T_DOCTYPE", + "$start", + "document", + "expressions", + "expression", + "tag", + "doctype", + "tag_start", + "tag_end", + "tag_body", + "whitespaces", + "whitespace" ] + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +def _reduce_1(val, _values) + s(:document, val[0]) +end + +def _reduce_2(val, _values) + s(:document) +end + +def _reduce_3(val, _values) + val.compact +end + +def _reduce_4(val, _values) + val[0] +end + +# reduce 5 omitted + +# reduce 6 omitted + +def _reduce_7(val, _values) + s(:doctype, val[0]) +end + +def _reduce_8(val, _values) + val[1] +end + +# reduce 9 omitted + +def _reduce_10(val, _values) + s(:element, val[0], val[1]) + +end + +# reduce 11 omitted + +# reduce 12 omitted + +# reduce 13 omitted + +# reduce 14 omitted + +# reduce 15 omitted + +def _reduce_none(val, _values) + val[0] +end + + end # class Parser + end # module Oga diff --git a/lib/oga/parser/html.y b/lib/oga/parser.y similarity index 98% rename from lib/oga/parser/html.y rename to lib/oga/parser.y index ada4bbd..1ff0cd9 100644 --- a/lib/oga/parser/html.y +++ b/lib/oga/parser.y @@ -1,4 +1,4 @@ -class Oga::Parser::HTML +class Oga::Parser token T_SPACE T_NEWLINE T_SMALLER T_GREATER T_SLASH token T_DQUOTE T_SQUOTE T_DASH T_RBRACKET T_LBRACKET diff --git a/spec/support/parsing.rb b/spec/support/parsing.rb index 72c81d5..1488add 100644 --- a/spec/support/parsing.rb +++ b/spec/support/parsing.rb @@ -28,7 +28,7 @@ module Oga # @return [Oga::AST::Node] # def parse_html(input) - return Oga::Parser::HTML.new.parse(input) + return Oga::Parser.new.parse(input) end end # ParsingHelpers end # Oga