Class: Glyph::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/glyph/parser.rb

Overview

The Glyph::Parser class can parse a string of text containing Glyph macros and produce the corresponding syntax tree.

Since:

  • 0.3.0

Instance Method Summary collapse

Constructor Details

#initialize(text, source_name = "--") ⇒ Parser

Initializes the parser.

Parameters:

  • text (String)

    the text to parse

  • source_name (String) (defaults to: "--")

    the name of the source file (stored in the root node)

Since:

  • 0.3.0



16
17
18
19
20
21
22
23
# File 'lib/glyph/parser.rb', line 16

def initialize(text, source_name="--")
	@ruby19 = RUBY_VERSION >= '1.9' ? true : false
	@source_name = source_name || "--"
	@input = StringScanner.new text
	@output = create_node DocumentNode, :name => @source_name.to_sym
	@current_macro = nil
	@current_attribute = nil
end

Instance Method Details

#parseGlyph::SyntaxNode

Parses the string of text provided during initialization

Returns:

Since:

  • 0.3.0



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/glyph/parser.rb', line 28

def parse
	count = 0
	while result = parse_contents(@output) do
		@output << result
		count +=1
	end
	if @input.pos < @input.string.bytesize then
		current_char = @input.string[@input.pos].chr rescue nil
		illegal_delimiter = current_char.match(/\]|\[/) rescue nil
		error "Macro delimiter '#{current_char}' not escaped" if illegal_delimiter
	end
	@output
end