Class: Rattler::Runtime::Parser

Inherits:
Util::Node show all
Defined in:
lib/rattler/runtime/parser.rb

Overview

Parser is the base class for all parsers in the Rattler framework.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Util::Node

#==, #[], [], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

#initialize(source, options = {}) ⇒ Parser

Create a new parser to parse source.

Parameters:

  • source (String)

    the source to parse

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

    a customizable set of options

Options Hash (options):

  • :tab_size (Integer) — default: 8

    tab size to use to calculate column numbers



33
34
35
36
37
# File 'lib/rattler/runtime/parser.rb', line 33

def initialize(source, options={})
  @source = source
  @scanner = StringScanner.new(source)
  @tab_size = options[:tab_size]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Instance Attribute Details

#sourceString (readonly)

Returns the source that this parser parses.

Returns:

  • (String)

    the source that this parser parses



40
41
42
# File 'lib/rattler/runtime/parser.rb', line 40

def source
  @source
end

Class Method Details

.parse!(source, options = {}) ⇒ Object

Parse source and raise a SyntaxError if the parse fails.

Parameters:

  • source (String)

    the source to parse

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

    a customizable set of options

Returns:

  • the parse result

Raises:



14
15
16
# File 'lib/rattler/runtime/parser.rb', line 14

def self.parse!(source, options={})
  self.new(source, options).parse!
end

.parse_fully!(source, options = {}) ⇒ Object

Parse the entirety of source and raise a SyntaxError if the parse fails.

Parameters:

  • source (String)

    the source to parse

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

    a customizable set of options

Returns:

  • the parse result

Raises:



24
25
26
# File 'lib/rattler/runtime/parser.rb', line 24

def self.parse_fully!(source, options={})
  self.new(source, options).parse_fully!
end

Instance Method Details

#failfalse

Fail and register a parse failure, unless a failure has already occurred at the same or later position in the source.

Yield Returns:

  • (String, Symbol)

    a failure message or rule name

Returns:

  • (false)

See Also:



96
97
98
99
100
101
# File 'lib/rattler/runtime/parser.rb', line 96

def fail # :yield:
  pos = @scanner.pos
  unless failure? and @failure_pos >= pos
    register_failure pos, (block_given? ? yield : nil)
  end
end

#fail!false

Fail and register a parse failure, unless a failure has already occurred at a later position in the source.

Yield Returns:

  • (String, Symbol)

    a failure message or rule name

Returns:

  • (false)


109
110
111
112
113
114
# File 'lib/rattler/runtime/parser.rb', line 109

def fail! # :yield:
  pos = @scanner.pos
  unless failure? and @failure_pos > pos
    register_failure pos, (block_given? ? yield : nil)
  end
end

#fail_parsefalse

Fail the same as #fail but cause the entire parse to fail immediately.

Yield Returns:

  • (String, Symbol)

    a failure message or rule name

Returns:

  • (false)


122
123
124
125
126
127
128
129
# File 'lib/rattler/runtime/parser.rb', line 122

def fail_parse
  if block_given?
    fail! { yield }
  else
    fail!
  end
  throw :parse_failed
end

#failureParseFailure

Return the last parse failure

Returns:



139
140
141
142
143
# File 'lib/rattler/runtime/parser.rb', line 139

def failure
  if failure?
    @__failure__ ||= ParseFailure.new(source, @failure_pos, @failure_msg)
  end
end

#failure?Boolean

Return true if there is a parse failure

Returns:

  • (Boolean)

    true if there is a parse failure



133
134
135
# File 'lib/rattler/runtime/parser.rb', line 133

def failure?
  !@failure_pos.nil?
end

#parseObject

Parse or register a parse failure

Returns:

  • the parse result



45
46
47
48
# File 'lib/rattler/runtime/parser.rb', line 45

def parse
  catch(:parse_failed) { return finish __parse__ }
  false
end

#parse!Object

Parse or raise a SyntaxError

Returns:

  • the parse result

Raises:



55
56
57
# File 'lib/rattler/runtime/parser.rb', line 55

def parse!
  parse or raise_error
end

#parse_fullyObject

Parse the entire source or register a parse failure

Returns:

  • the parse result if the entire source was matched



62
63
64
# File 'lib/rattler/runtime/parser.rb', line 62

def parse_fully
  (result = parse) && (@scanner.eos? || fail { :EOF }) && result
end

#parse_fully!Object

Parse the entire source or raise a SyntaxError

Returns:

  • the parse result if the entire source was matched

Raises:



72
73
74
# File 'lib/rattler/runtime/parser.rb', line 72

def parse_fully!
  parse_fully or raise_error
end

#posFixnum

Returns the current parse position.

Returns:

  • (Fixnum)

    the current parse position



77
78
79
# File 'lib/rattler/runtime/parser.rb', line 77

def pos
  @scanner.pos
end

#pos=(n) ⇒ Fixnum

Set the current parse position

Parameters:

  • n (Integer)

    the new parse position

Returns:

  • (Fixnum)

    the new parse position



84
85
86
# File 'lib/rattler/runtime/parser.rb', line 84

def pos=(n)
  @scanner.pos = n
end