2014-02-26 18:50:16 +00:00
|
|
|
module Oga
|
|
|
|
module ParsingHelpers
|
|
|
|
##
|
|
|
|
# Builds an AST node.
|
|
|
|
#
|
|
|
|
# @param [Symbol] type
|
|
|
|
# @param [Array] cihldren
|
|
|
|
# @return [Oga::AST::Node]
|
|
|
|
#
|
|
|
|
def s(type, *children)
|
|
|
|
return Oga::AST::Node.new(type, children)
|
|
|
|
end
|
2014-02-26 20:36:30 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Lexes a string and returns the tokens.
|
|
|
|
#
|
|
|
|
# @param [String] input
|
2014-03-16 22:42:24 +00:00
|
|
|
# @param [Hash] options
|
2014-02-26 20:36:30 +00:00
|
|
|
# @return [Array]
|
|
|
|
#
|
2014-03-16 22:42:24 +00:00
|
|
|
def lex(input, options = {})
|
2014-04-09 20:08:13 +00:00
|
|
|
return Oga::XML::Lexer.new(input, options).lex
|
2014-02-26 20:36:30 +00:00
|
|
|
end
|
2014-02-27 00:27:43 +00:00
|
|
|
|
|
|
|
##
|
2014-03-25 08:40:24 +00:00
|
|
|
# Parses the given XML and returns an AST.
|
2014-02-27 00:27:43 +00:00
|
|
|
#
|
|
|
|
# @param [String] input
|
2014-03-16 22:46:20 +00:00
|
|
|
# @param [Hash] options
|
2014-02-27 00:27:43 +00:00
|
|
|
# @return [Oga::AST::Node]
|
|
|
|
#
|
2014-03-16 22:46:20 +00:00
|
|
|
def parse(input, options = {})
|
2014-04-09 20:08:13 +00:00
|
|
|
return Oga::XML::Parser.new(input, options).parse
|
2014-02-27 00:27:43 +00:00
|
|
|
end
|
2014-03-25 08:40:24 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Parses the given HTML and returns an AST.
|
|
|
|
#
|
|
|
|
# @see #parse
|
|
|
|
#
|
|
|
|
def parse_html(input, options = {})
|
2014-04-09 20:08:13 +00:00
|
|
|
return Oga::HTML::Parser.new(input, options).parse
|
2014-03-25 08:40:24 +00:00
|
|
|
end
|
2014-04-07 19:31:36 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Parses the given invalid XML and returns the error message.
|
|
|
|
#
|
|
|
|
# @param [String] xml
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
def parse_error(xml)
|
|
|
|
parse(xml)
|
|
|
|
rescue Racc::ParseError => error
|
|
|
|
return error.message
|
|
|
|
end
|
2014-02-26 18:50:16 +00:00
|
|
|
end # ParsingHelpers
|
|
|
|
end # Oga
|