Class: DMark::Lexer
- Inherits:
-
Object
- Object
- DMark::Lexer
- Defined in:
- lib/dmark/lexer.rb
Defined Under Namespace
Classes: LexerError
Constant Summary collapse
- INDENTATION =
2
Instance Method Summary collapse
-
#initialize(string) ⇒ Lexer
constructor
A new instance of Lexer.
- #run ⇒ Object
Constructor Details
#initialize(string) ⇒ Lexer
Returns a new instance of Lexer.
5 6 7 8 9 10 11 |
# File 'lib/dmark/lexer.rb', line 5 def initialize(string) @string = string @element_stack = [] @tokens = [] @pending_blanks = 0 end |
Instance Method Details
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dmark/lexer.rb', line 13 def run @string.lines.each_with_index do |line, line_nr| case line when /^\s+$/ # blank line @pending_blanks += 1 when /^(\s*)([a-z0-9-]+)(\[(.*?)\])?\.\s*$/ # empty element indentation = Regexp.last_match[1] element = Regexp.last_match[2] attributes = parse_attributes(Regexp.last_match[4]) unwind_stack_until(indentation.size) @element_stack << element @tokens << DMark::Tokens::TagBeginToken.new(name: element, attributes: attributes) when /^(\s*)([a-z0-9-]+)(\[(.*?)\])?\. (.*)$/ # element with inline content indentation = Regexp.last_match[1] element = Regexp.last_match[2] attributes = parse_attributes(Regexp.last_match[4]) data = Regexp.last_match[5] unwind_stack_until(indentation.size) @tokens << DMark::Tokens::TagBeginToken.new(name: element, attributes: attributes) @tokens.concat(lex_inline(data, line_nr + 1)) @tokens << DMark::Tokens::TagEndToken.new(name: element) when /^(\s*)(.*)$/ # other line (e.g. data) indentation = Regexp.last_match[1] data = Regexp.last_match[2] unwind_stack_until(indentation.size) if @element_stack.empty? # FIXME: unify format of messages (uppercase, lowercase, …) raise LexerError.new("Can’t insert raw data at root level", line, line_nr, 1) end extra_indentation = [indentation.size - INDENTATION * @element_stack.size, 0].max @tokens.concat(lex_inline(' ' * extra_indentation + data + "\n", line_nr + 1)) end end unwind_stack_until(0) @tokens end |