Basic documentation for Oga::Parser.
This commit is contained in:
parent
19c1d66287
commit
52abc9d29e
|
@ -1,3 +1,12 @@
|
||||||
|
##
|
||||||
|
# Low level AST parser that supports both XML and HTML.
|
||||||
|
#
|
||||||
|
# Note that this parser itself does not deal with special HTML void elements.
|
||||||
|
# It requires every tag to have a closing tag. As such you'll need to enable
|
||||||
|
# HTML parsing mode when parsing HTML. This can be done as following:
|
||||||
|
#
|
||||||
|
# parser = Oga::Parser.new(:html => true)
|
||||||
|
#
|
||||||
class Oga::Parser
|
class Oga::Parser
|
||||||
|
|
||||||
token T_STRING T_TEXT
|
token T_STRING T_TEXT
|
||||||
|
@ -118,16 +127,30 @@ rule
|
||||||
end
|
end
|
||||||
|
|
||||||
---- inner
|
---- inner
|
||||||
|
##
|
||||||
|
# @param [Hash] options
|
||||||
|
#
|
||||||
|
# @option options [TrueClass|FalseClass] :html Enables HTML parsing mode.
|
||||||
|
# @see Oga::Lexer#initialize
|
||||||
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@lexer = Lexer.new(options)
|
@lexer = Lexer.new(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Resets the internal state of the parser.
|
||||||
|
#
|
||||||
def reset
|
def reset
|
||||||
@lines = []
|
@lines = []
|
||||||
@line = 1
|
@line = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Emits a new AST token.
|
||||||
|
#
|
||||||
|
# @param [Symbol] type
|
||||||
|
# @param [Array] children
|
||||||
|
#
|
||||||
def s(type, *children)
|
def s(type, *children)
|
||||||
return AST::Node.new(
|
return AST::Node.new(
|
||||||
type,
|
type,
|
||||||
|
@ -136,6 +159,11 @@ end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns the next token from the lexer.
|
||||||
|
#
|
||||||
|
# @return [Array]
|
||||||
|
#
|
||||||
def next_token
|
def next_token
|
||||||
type, value, line = @tokens.shift
|
type, value, line = @tokens.shift
|
||||||
|
|
||||||
|
@ -144,6 +172,12 @@ end
|
||||||
return type ? [type, value] : [false, false]
|
return type ? [type, value] : [false, false]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Fixnum] type The type of token the error occured on.
|
||||||
|
# @param [String] value The value of the token.
|
||||||
|
# @param [Array] stack The current stack of parsed nodes.
|
||||||
|
# @raise [Racc::ParseError]
|
||||||
|
#
|
||||||
def on_error(type, value, stack)
|
def on_error(type, value, stack)
|
||||||
name = token_to_str(type)
|
name = token_to_str(type)
|
||||||
index = @line - 1
|
index = @line - 1
|
||||||
|
@ -172,6 +206,16 @@ Unexpected #{name} with value #{value.inspect} on line #{@line}:
|
||||||
EOF
|
EOF
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Parses the supplied string and returns the AST.
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# parser = Oga::Parser.new
|
||||||
|
# ast = parser.parse('<foo>bar</foo>')
|
||||||
|
#
|
||||||
|
# @param [String] string
|
||||||
|
# @return [Oga::AST::Node]
|
||||||
|
#
|
||||||
def parse(string)
|
def parse(string)
|
||||||
@lines = string.lines
|
@lines = string.lines
|
||||||
@tokens = @lexer.lex(string)
|
@tokens = @lexer.lex(string)
|
||||||
|
|
Loading…
Reference in New Issue