oga/spec/support/parsing.rb

94 lines
1.8 KiB
Ruby
Raw Normal View History

2014-02-26 18:50:16 +00:00
module Oga
module ParsingHelpers
##
# Builds an AST node.
#
# @param [Symbol] type
# @param [Array] cihldren
# @return [AST::Node]
2014-02-26 18:50:16 +00:00
#
def s(type, *children)
return AST::Node.new(type, children)
2014-02-26 18:50:16 +00:00
end
##
# Lexes a string and returns the tokens.
#
# @param [String] input
# @param [Hash] options
# @return [Array]
#
def lex(input, options = {})
return Oga::XML::Lexer.new(input, options).lex
end
##
# Lexes an XPath expression.
#
# @param [String] input
# @return [Array]
#
def lex_xpath(input)
return Oga::XPath::Lexer.new(input).lex
end
##
# Lexes a CSS expression.
#
# @param [String] input
# @return [Array]
#
def lex_css(input)
return Oga::CSS::Lexer.new(input).lex
end
2014-10-04 23:28:31 +00:00
##
# @param [String] input
# @return [AST::Node]
#
def parse_css(input)
return Oga::CSS::Parser.new(input).parse
end
##
# Parses an XPath expression.
#
# @param [String] input
# @return [AST::Node]
#
def parse_xpath(input)
return Oga::XPath::Parser.new(input).parse
end
##
# @param [String] input
2014-03-16 22:46:20 +00:00
# @param [Hash] options
# @return [Oga::XML::Document]
#
2014-03-16 22:46:20 +00:00
def parse(input, options = {})
return Oga::XML::Parser.new(input, options).parse
end
##
# Parses the given HTML and returns an AST.
#
# @see #parse
#
def parse_html(input, options = {})
return Oga::HTML::Parser.new(input, options).parse
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