Class: FilterLexer::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_lexer.rb

Overview

The parser is your main entrypoint for the lexer

You never instantiate it, you just use it’s static methods

Class Method Summary collapse

Class Method Details

.output_tree(element, indent = '') ⇒ Object

When using the lexer, it may be useful to have a visual representation of the tree

Just pass the tree (or any node in it, if you’re only interested in that part) to this function, and a visual representation of the tree will magically appear in stdout



39
40
41
42
43
44
# File 'lib/filter_lexer.rb', line 39

def output_tree(element, indent = '')
	puts "#{indent}#{element.class}: #{element.text_value}"
	(element.elements || []).each do |e|
		output_tree(e, "#{indent}  ")
	end
end

.parse(data) ⇒ Object

The bread and butter of the entire thing: parsing, and it’s pretty simple

Just pass in a string, and out comes a parsed tree, or an exception

The parsed tree will always be an FilterLexer::Expression object The exception will always be an FilterLexer::ParseException object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/filter_lexer.rb', line 22

def parse(data)
	# Pass the data over to the parser instance
	tree = @parser.parse(data)

	# If the AST is nil then there was an error during parsing
	fail ParseException, @parser if tree.nil?

	# Cleanup the tree
	clean_tree(tree)

	return tree
end