lib/d-mark/parser.rb in d-mark-0.2.0 vs lib/d-mark/parser.rb in d-mark-1.0.0a1
- old
+ new
@@ -11,45 +11,10 @@
super("parse error at line #{@line_nr + 1}, col #{@col_nr + 1}: #{@msg}")
end
end
- class ElementNode
- attr_reader :name
- attr_reader :attributes
- attr_reader :children
-
- def initialize(name, attributes, children)
- @name = name
- @attributes = attributes
- @children = children
- end
-
- def inspect
- io = ''
- io << 'Element(' << @name << ', '
- if @attributes.any?
- io << @attributes.inspect
- io << ', '
- end
- io << @children.inspect
- io << ')'
- io
- end
-
- def ==(other)
- case other
- when ElementNode
- @name == other.name &&
- @children == other.children &&
- @attributes == other.attributes
- else
- false
- end
- end
- end
-
attr_reader :pos
def initialize(input)
@input = input
@input_chars = @input.chars
@@ -62,10 +27,21 @@
def parse
res = []
loop do
break if eof?
+
+ blank_pos = try_read_blank_line
+ break unless blank_pos
+
+ @pos = blank_pos
+ @line_nr += 1
+ @col_nr = 0
+ end
+
+ loop do
+ break if eof?
res << read_block_with_children
end
res
end
@@ -159,18 +135,18 @@
def try_read_block_start
old_pos = @pos
success =
if try_read_identifier_head
- if try_read_identifier_tail
- case peek_char
- when '['
- true
- when '.'
- advance
- [' ', "\n", nil].include?(peek_char)
- end
+ read_identifier_tail
+
+ case peek_char
+ when '['
+ true
+ when '.'
+ advance
+ [' ', "\n", nil].include?(peek_char)
end
end
@pos = old_pos
success
@@ -184,28 +160,10 @@
advance
char
end
end
- # FIXME: ugly and duplicated
- def try_read_identifier_tail
- res = ''
-
- loop do
- char = peek_char
- case char
- when 'a'..'z', '-', '0'..'9'
- advance
- res << char
- else
- break
- end
- end
-
- res.to_s
- end
-
def detect_indentation
indentation_chars = 0
pos = @pos
loop do
@@ -219,30 +177,10 @@
end
indentation_chars / 2
end
- def read_until_eol_or_eof
- res = ''
-
- loop do
- char = peek_char
- case char
- when "\n"
- advance
- break
- when nil
- break
- else
- advance
- res << char
- end
- end
-
- res.to_s
- end
-
def read_indentation(indentation)
indentation.times do
read_char(' ')
read_char(' ')
end
@@ -361,23 +299,31 @@
loop do
char = peek_char
if is_escaping
case char
- when nil, "\n"
- break
- else
+ when '%', ']', ','
advance
res << char
is_escaping = false
+ when nil
+ raise_parse_error('unexpected file end in attribute value')
+ when "\n"
+ raise_parse_error('unexpected line break in attribute value')
+ else
+ raise_parse_error(%(expected "%", "," or "]" after "%", but got #{char.inspect}))
end
else
case char
- when nil, "\n", ']', ','
+ when ']', ','
break
when '%'
advance
is_escaping = true
+ when nil
+ raise_parse_error('unexpected file end in attribute value')
+ when "\n"
+ raise_parse_error('unexpected line break in attribute value')
else
advance
res << char
end
end