Class: Cadenza::Parser

Inherits:
RaccParser show all
Defined in:
lib/cadenza/parser.rb

Overview

The Parser class takes all tokens retrieved from it's lexer and forms them into an abstract syntax tree (AST) with a DocumentNode at it's root.

Parser and RaccParser are tightly coupled to each other but were separated since Racc tends to generate undocumentable parser classes.

Constant Summary

Constants inherited from RaccParser

RaccParser::Racc_arg, RaccParser::Racc_debug_parser, RaccParser::Racc_token_to_s_table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RaccParser

#_reduce_none

Constructor Details

#initialize(options = {}) ⇒ Parser

creates a new Cadenza::Parser with the given options

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :lexer (Lexer) — default: Cadenza::Lexer.new

    the lexer you want this parser to retrieve tokens from.

Raises:

  • (RuntimeError)

    if the given lexer doesn't respond to :next_token and :source=



20
21
22
23
24
25
26
# File 'lib/cadenza/parser.rb', line 20

def initialize(options={})
  @lexer = options.fetch(:lexer, Cadenza::Lexer.new)

  raise "Lexers passed to the parser must define next_token" unless @lexer.respond_to?(:next_token)

  raise "Lexers passed to the parser must define source=" unless @lexer.respond_to?(:source=)
end

Instance Attribute Details

#lexerLexer (readonly)

Returns the lexer object this parser is using to retrieve tokens from

Returns:

  • (Lexer)

    the lexer object this parser is using to retrieve tokens from



13
14
15
# File 'lib/cadenza/parser.rb', line 13

def lexer
  @lexer
end

Instance Method Details

#parse(source) ⇒ DocumentNode

takes the given source object and parses tokens from it, the tokens are then constructed into an abstract syntax tree (AST) with a DocumentNode at the root. The root node is then returned.

Parameters:

  • source (String)

    the template text to parse

Returns:

Raises:

  • (ParseError)

    if the given template does not have a valid syntax



35
36
37
38
39
40
41
42
43
# File 'lib/cadenza/parser.rb', line 35

def parse(source)
  @lexer.source = source

  @stack = [DocumentNode.new]

  do_parse

  @stack.first
end