Class: Parslet::Accelerator::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/parslet/accelerator.rb

Overview

An expression to match against a tree of parser atoms. Normally, an expression is produced by Parslet::Accelerator.any, Parslet::Accelerator.str or Parslet::Accelerator.re.

Expressions can be chained much like parslet atoms can be:

expr.repeat(1)      # matching repetition
expr.absent?        # matching absent?
expr.present?       # matching present?
expr1 >> expr2      # matching a sequence
expr1 | expr2       # matching an alternation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, *args) ⇒ Expression

Returns a new instance of Expression.



41
42
43
44
# File 'lib/parslet/accelerator.rb', line 41

def initialize(type, *args)
  @type = type
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



39
40
41
# File 'lib/parslet/accelerator.rb', line 39

def args
  @args
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/parslet/accelerator.rb', line 38

def type
  @type
end

Instance Method Details

#>>(other_expr) ⇒ Expression

Returns:



47
48
49
# File 'lib/parslet/accelerator.rb', line 47

def >> other_expr
  join_or_new :seq, other_expr
end

#absent?Expression

Returns:



57
58
59
# File 'lib/parslet/accelerator.rb', line 57

def absent?
  Expression.new(:absent, self)
end

#as(name) ⇒ Expression

Returns:



71
72
73
# File 'lib/parslet/accelerator.rb', line 71

def as name
  Expression.new(:as, name)
end

#join_or_new(tag, other_expr) ⇒ Expression

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



77
78
79
80
81
82
83
84
# File 'lib/parslet/accelerator.rb', line 77

def join_or_new tag, other_expr
  if type == tag
    @args << other_expr
    self
  else
    Expression.new(tag, self, other_expr)
  end
end

#present?Expression

Returns:



61
62
63
# File 'lib/parslet/accelerator.rb', line 61

def present?
  Expression.new(:present, self)
end

#repeat(min = 0, max = nil) ⇒ Expression

Returns:



66
67
68
# File 'lib/parslet/accelerator.rb', line 66

def repeat min=0, max=nil
  Expression.new(:rep, min, max, self)
end

#|(other_expr) ⇒ Expression

Returns:



52
53
54
# File 'lib/parslet/accelerator.rb', line 52

def | other_expr
  join_or_new :alt, other_expr
end