Class: Whittle::Terminal

Inherits:
Rule
  • Object
show all
Defined in:
lib/whittle/terminal.rb

Overview

Represents an terminal Rule, matching a pattern in the input String

Constant Summary

Constants inherited from Rule

Rule::DUMP_ACTION, Rule::NULL_ACTION

Instance Attribute Summary

Attributes inherited from Rule

#action, #assoc, #components, #name, #prec

Instance Method Summary collapse

Methods inherited from Rule

#%, #^, #as, #build_parse_table, #skip!

Instance Method Details

#scan(source, offset, line) ⇒ Hash

Invoked for terminal rules during lexing, ignored for nonterminal rules.

Returns nil if nothing is matched.

Parameters:

  • source (String)

    the input String the scan

  • offset (Fixnum)

    the current index in the search

  • line (Fixnum)

    the line the lexer was up to when the previous token was matched

Returns:

  • (Hash)

    a Hash representing the token, containing :rule, :value, :line and :discarded, if the token is to be skipped.



29
30
31
32
33
34
35
36
37
38
# File 'lib/whittle/terminal.rb', line 29

def scan(source, offset, line)
  if match = source.match(@pattern, offset)
    {
      :rule      => self,
      :value     => match[0],
      :line      => line + match[0].count("\r\n", "\n"),
      :discarded => @action.equal?(NULL_ACTION)
    }
  end
end

#terminal?Boolean

Hard-coded to always return true

Returns:

  • (Boolean)


9
10
11
# File 'lib/whittle/terminal.rb', line 9

def terminal?
  true
end