Class: FuzzBert::Template::Parser
- Inherits:
-
Object
- Object
- FuzzBert::Template::Parser
- Defined in:
- lib/fuzzbert/template.rb
Instance Method Summary collapse
- #determine_state ⇒ Object
-
#initialize(template) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parse_escape ⇒ Object
- #parse_identifier ⇒ Object
- #parse_text ⇒ Object
- #parse_token ⇒ Object
Constructor Details
#initialize(template) ⇒ Parser
Returns a new instance of Parser.
25 26 27 28 |
# File 'lib/fuzzbert/template.rb', line 25 def initialize(template) @io = StringIO.new(template) @template = [] end |
Instance Method Details
#determine_state ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fuzzbert/template.rb', line 49 def determine_state begin @buf = @io.readchar case @buf when '$' c = @io.readchar if c == "{" @buf = "" :IDENTIFIER else @buf << c :TEXT end when '\\' @buf = "" :TEXT else :TEXT end rescue EOFError :EOF end end |
#parse ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/fuzzbert/template.rb', line 30 def parse @state = determine_state while token = parse_token @template << token end @template end |
#parse_escape ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/fuzzbert/template.rb', line 119 def parse_escape begin @io.readchar rescue EOFError '\\' end end |
#parse_identifier ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fuzzbert/template.rb', line 74 def parse_identifier name = "" begin until (c = @io.readchar) == '}' name << c end if name.empty? raise RuntimeError.new("No identifier name given") end @state = determine_state Identifier.new(name) rescue EOFError raise RuntimeError.new("Unclosed identifier") end end |
#parse_text ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fuzzbert/template.rb', line 92 def parse_text text = @buf begin loop do until (c = @io.readchar) == '$' if c == '\\' text << parse_escape else text << c end end d = @io.readchar if d == "{" @state = :IDENTIFIER return Text.new(text) else text << c text << d end end rescue EOFError @state = :EOF Text.new(text) end end |
#parse_token ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fuzzbert/template.rb', line 38 def parse_token case @state when :TEXT parse_text when :IDENTIFIER parse_identifier else nil end end |