Class: Semlogr::Templates::Parser
- Inherits:
-
Object
- Object
- Semlogr::Templates::Parser
- Defined in:
- lib/semlogr/templates/parser.rb
Constant Summary collapse
- PROPERTY_TOKEN_START =
'{'.freeze
- PROPERTY_TOKEN_END =
'}'.freeze
Class Method Summary collapse
- .parse(template) ⇒ Object
- .parse_property_token(template, start) ⇒ Object
- .parse_text_token(template, start) ⇒ Object
Class Method Details
.parse(template) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/semlogr/templates/parser.rb', line 14 def self.parse(template) return Template::EMPTY unless template && !template.empty? cached_template = @template_cache[template] return cached_template if cached_template tokens = [] pos = 0 while pos < template.size text_token, pos = parse_text_token(template, pos) tokens.push(text_token) if text_token property_token, pos = parse_property_token(template, pos) tokens.push(property_token) if property_token end @template_cache[template] = Template.new(template, tokens) end |
.parse_property_token(template, start) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/semlogr/templates/parser.rb', line 52 def self.parse_property_token(template, start) return [nil, start] unless template[start] == PROPERTY_TOKEN_START token = nil pos = start while pos < template.size if template[pos] == PROPERTY_TOKEN_END raw_text = template[start..pos] property_name = raw_text[1..-2] token = PropertyToken.new(raw_text, property_name.to_sym) return [token, pos + 1] end pos += 1 end if pos > start text = template[start..pos - 1] token = TextToken.new(text) end [token, pos] end |
.parse_text_token(template, start) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/semlogr/templates/parser.rb', line 34 def self.parse_text_token(template, start) token = nil pos = start while pos < template.size break if template[pos] == PROPERTY_TOKEN_START pos += 1 end if pos > start text = template[start..pos - 1] token = TextToken.new(text) end [token, pos] end |