Class: Pdfrb::Arlington::Predicate::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/arlington/predicate/parser.rb

Overview

Recursive-descent parser. Builds AST from tokens. Per the Arlington grammar: && and || must be fully parenthesised (no precedence); we enforce that by only accepting them inside (...) groups.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



13
14
15
16
# File 'lib/pdfrb/arlington/predicate/parser.rb', line 13

def initialize(tokens)
  @tokens = tokens
  @pos = 0
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



11
12
13
# File 'lib/pdfrb/arlington/predicate/parser.rb', line 11

def ast
  @ast
end

#tokensObject (readonly)

Returns the value of attribute tokens.



11
12
13
# File 'lib/pdfrb/arlington/predicate/parser.rb', line 11

def tokens
  @tokens
end

Class Method Details

.parse(tokens_or_source) ⇒ Object



18
19
20
21
# File 'lib/pdfrb/arlington/predicate/parser.rb', line 18

def self.parse(tokens_or_source)
  toks = tokens_or_source.is_a?(::String) ? Lexer.tokenize(tokens_or_source) : tokens_or_source
  new(toks).parse
end

Instance Method Details

#parseObject

Raises:



23
24
25
26
27
28
29
30
# File 'lib/pdfrb/arlington/predicate/parser.rb', line 23

def parse
  return nil if tokens.empty?

  node = parse_expr
  raise Pdfrb::SyntaxError, "trailing tokens at pos #{@pos}" unless eof?

  node
end