2014-02-26 18:50:16 +00:00
|
|
|
module Oga
|
|
|
|
module ParsingHelpers
|
|
|
|
##
|
|
|
|
# Builds an AST node.
|
|
|
|
#
|
|
|
|
# @param [Symbol] type
|
|
|
|
# @param [Array] cihldren
|
2014-06-11 22:20:46 +00:00
|
|
|
# @return [AST::Node]
|
2014-02-26 18:50:16 +00:00
|
|
|
#
|
|
|
|
def s(type, *children)
|
2014-06-11 22:20:46 +00:00
|
|
|
# TODO: add support for CSS AST nodes.
|
|
|
|
return Oga::XPath::Node.new(type, children)
|
2014-02-26 18:50:16 +00:00
|
|
|
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-06-01 17:24:35 +00:00
|
|
|
##
|
|
|
|
# Lexes an XPath expression.
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
def lex_xpath(input)
|
|
|
|
return Oga::XPath::Lexer.new(input).lex
|
|
|
|
end
|
|
|
|
|
2014-09-16 14:32:57 +00:00
|
|
|
##
|
|
|
|
# Lexes a CSS expression.
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
def lex_css(input)
|
|
|
|
return Oga::CSS::Lexer.new(input).lex
|
|
|
|
end
|
|
|
|
|
2014-06-11 22:20:46 +00:00
|
|
|
##
|
|
|
|
# Parses an XPath expression.
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
# @return [Oga::XPath::Node]
|
|
|
|
#
|
|
|
|
def parse_xpath(input)
|
|
|
|
return Oga::XPath::Parser.new(input).parse
|
|
|
|
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
|