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.

Author:

  • Jason Arhart

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, #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 (Int) — default: 8

    tab size to use to calculate column numbers



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

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)

The source that this parser parses

Returns:

  • (String)

    the source that this parser parses



51
52
53
# File 'lib/rattler/runtime/parser.rb', line 51

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:



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

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:



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

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:



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

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)


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

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)


134
135
136
137
138
139
140
141
# File 'lib/rattler/runtime/parser.rb', line 134

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

#failureParseFailure

Return the last parse failure

Returns:



151
152
153
154
155
# File 'lib/rattler/runtime/parser.rb', line 151

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



145
146
147
# File 'lib/rattler/runtime/parser.rb', line 145

def failure?
  !@failure_pos.nil?
end

#parseObject

Parse or register a parse failure

Returns:

  • the parse result



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

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

#parse!Object

Parse or raise a SyntaxError

Returns:

  • the parse result

Raises:



66
67
68
# File 'lib/rattler/runtime/parser.rb', line 66

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



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

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:



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

def parse_fully!
  parse_fully or raise_error
end

#posInteger

The current parse position

Returns:

  • (Integer)

    the current parse position



89
90
91
# File 'lib/rattler/runtime/parser.rb', line 89

def pos
  @scanner.pos
end

#pos=(n) ⇒ Integer

Set the current parse position

Parameters:

  • n (Integer)

    the new parse position

Returns:

  • (Integer)

    n



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

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