Class: Prosereflect::Schema::ContentMatch::TokenStream

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/schema/content_match.rb

Overview

Token stream for parsing content expressions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, node_types) ⇒ TokenStream

Returns a new instance of TokenStream.



600
601
602
603
604
605
# File 'lib/prosereflect/schema/content_match.rb', line 600

def initialize(string, node_types)
  @string = string
  @node_types = node_types
  @pos = 0
  @tokens = tokenize(string)
end

Instance Attribute Details

#node_typesObject (readonly)

Returns the value of attribute node_types.



598
599
600
# File 'lib/prosereflect/schema/content_match.rb', line 598

def node_types
  @node_types
end

#stringObject (readonly)

Returns the value of attribute string.



598
599
600
# File 'lib/prosereflect/schema/content_match.rb', line 598

def string
  @string
end

Instance Method Details

#accept(tok) ⇒ Object



615
616
617
618
619
620
621
622
# File 'lib/prosereflect/schema/content_match.rb', line 615

def accept(tok)
  if peek == tok
    @pos += 1
    true
  else
    false
  end
end

#advanceObject



611
612
613
# File 'lib/prosereflect/schema/content_match.rb', line 611

def advance
  @tokens[@pos]&.tap { @pos += 1 }
end

#error(message) ⇒ Object



643
644
645
646
# File 'lib/prosereflect/schema/content_match.rb', line 643

def error(message)
  raise Prosereflect::SchemaErrors::ContentMatchError,
        "#{message} (in content expression) \"#{@string}\""
end

#expect(tok) ⇒ Object



624
625
626
627
628
629
630
631
# File 'lib/prosereflect/schema/content_match.rb', line 624

def expect(tok)
  unless accept(tok)
    raise Prosereflect::SchemaErrors::ContentMatchError,
          "Expected #{tok}, got #{peek.inspect}"
  end

  true
end

#expect_numberObject



633
634
635
636
637
638
639
640
641
# File 'lib/prosereflect/schema/content_match.rb', line 633

def expect_number
  tok = peek
  unless /^\d+$/.match?(tok)
    raise Prosereflect::SchemaErrors::ContentMatchError,
          "Expected number, got #{tok.inspect}"
  end

  advance.to_i
end

#peekObject



607
608
609
# File 'lib/prosereflect/schema/content_match.rb', line 607

def peek
  @tokens[@pos]
end