Class: Rbexy::Lexer
- Inherits:
-
Object
- Object
- Rbexy::Lexer
- Defined in:
- lib/rbexy/lexer.rb
Defined Under Namespace
Classes: SyntaxError
Constant Summary collapse
- Patterns =
HashMash.new( open_expression: /{/, close_expression: /}/, expression_content: /[^}{"'<]+/, open_tag_def: /<(?!\/)/, open_tag_end: /<\//, close_tag: /\s*\/?>/, close_self_closing_tag: /\s*\/>/, tag_name: /\/?[A-Za-z0-9\-_.]+/, text_content: /[^<{#]+/, comment: /^\p{Blank}*#.*(\n|\z)/, whitespace: /\s+/, attr: /[A-Za-z0-9\-_\.:]+/, open_attr_splat: /{\*\*/, attr_assignment: /=/, double_quote: /"/, single_quote: /'/, double_quoted_text_content: /[^"]+/, single_quoted_text_content: /[^']+/, expression_internal_tag_prefixes: /(\s+(&&|\?|:|do|do\s*\|[^\|]+\||{|{\s*\|[^\|]+\|)\s+\z|\A\s*\z)/, declaration: /<![^>]*>/ )
Instance Attribute Summary collapse
-
#curr_default_text ⇒ Object
Returns the value of attribute curr_default_text.
-
#curr_expr ⇒ Object
Returns the value of attribute curr_expr.
-
#curr_expr_bracket_levels ⇒ Object
Returns the value of attribute curr_expr_bracket_levels.
-
#curr_expr_quote_levels ⇒ Object
readonly
Returns the value of attribute curr_expr_quote_levels.
-
#curr_quoted_text ⇒ Object
Returns the value of attribute curr_quoted_text.
-
#scanner ⇒ Object
readonly
Returns the value of attribute scanner.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #expression_content? ⇒ Boolean
- #expression_inner_bracket ⇒ Object
- #expression_inner_double_quote ⇒ Object
- #expression_inner_single_quote ⇒ Object
- #expression_quoted_string_content ⇒ Object
-
#initialize(code) ⇒ Lexer
constructor
A new instance of Lexer.
- #open_expression ⇒ Object
- #open_tag_def ⇒ Object
- #potential_expression_inner_tag ⇒ Object
- #tokenize ⇒ Object
Constructor Details
#initialize(code) ⇒ Lexer
Returns a new instance of Lexer.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rbexy/lexer.rb', line 40 def initialize(code) @stack = [:default] @curr_expr_bracket_levels = 0 @curr_expr_quote_levels = { single: 0, double: 0 } @curr_expr = "" @curr_default_text = "" @curr_quoted_text = "" @tokens = [] @scanner = StringScanner.new(code) end |
Instance Attribute Details
#curr_default_text ⇒ Object
Returns the value of attribute curr_default_text.
37 38 39 |
# File 'lib/rbexy/lexer.rb', line 37 def curr_default_text @curr_default_text end |
#curr_expr ⇒ Object
Returns the value of attribute curr_expr.
37 38 39 |
# File 'lib/rbexy/lexer.rb', line 37 def curr_expr @curr_expr end |
#curr_expr_bracket_levels ⇒ Object
Returns the value of attribute curr_expr_bracket_levels.
37 38 39 |
# File 'lib/rbexy/lexer.rb', line 37 def curr_expr_bracket_levels @curr_expr_bracket_levels end |
#curr_expr_quote_levels ⇒ Object (readonly)
Returns the value of attribute curr_expr_quote_levels.
36 37 38 |
# File 'lib/rbexy/lexer.rb', line 36 def curr_expr_quote_levels @curr_expr_quote_levels end |
#curr_quoted_text ⇒ Object
Returns the value of attribute curr_quoted_text.
37 38 39 |
# File 'lib/rbexy/lexer.rb', line 37 def curr_quoted_text @curr_quoted_text end |
#scanner ⇒ Object (readonly)
Returns the value of attribute scanner.
36 37 38 |
# File 'lib/rbexy/lexer.rb', line 36 def scanner @scanner end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
36 37 38 |
# File 'lib/rbexy/lexer.rb', line 36 def stack @stack end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
36 37 38 |
# File 'lib/rbexy/lexer.rb', line 36 def tokens @tokens end |
Instance Method Details
#expression_content? ⇒ Boolean
279 280 281 282 283 284 285 286 287 288 |
# File 'lib/rbexy/lexer.rb', line 279 def expression_content? # Patterns.expression_content ends at `<` characters, because we need to # separately scan for allowed open_tag_defs within expressions. We should # support any found open_tag_ends as expression content, as that means the # open_tag_def was not considered allowed (or stack would be inside # :tag_def instead of :expression) so we should thus also consider the # open_tag_end to just be a part of the expression (maybe its in a string, # etc). scanner.scan(Patterns.expression_content) || scanner.scan(Patterns.open_tag_end) end |
#expression_inner_bracket ⇒ Object
259 260 261 262 |
# File 'lib/rbexy/lexer.rb', line 259 def expression_inner_bracket self.curr_expr += scanner.matched stack.push(:expression_inner_bracket) end |
#expression_inner_double_quote ⇒ Object
264 265 266 267 |
# File 'lib/rbexy/lexer.rb', line 264 def expression_inner_double_quote self.curr_expr += scanner.matched stack.push(:expression_inner_double_quote) end |
#expression_inner_single_quote ⇒ Object
269 270 271 272 |
# File 'lib/rbexy/lexer.rb', line 269 def expression_inner_single_quote self.curr_expr += scanner.matched stack.push(:expression_inner_single_quote) end |
#expression_quoted_string_content ⇒ Object
274 275 276 277 |
# File 'lib/rbexy/lexer.rb', line 274 def expression_quoted_string_content self.curr_expr += scanner.getch stack.pop unless curr_expr.end_with?('\\') end |
#open_expression ⇒ Object
254 255 256 257 |
# File 'lib/rbexy/lexer.rb', line 254 def open_expression tokens << [:OPEN_EXPRESSION] stack.push(:expression) end |
#open_tag_def ⇒ Object
249 250 251 252 |
# File 'lib/rbexy/lexer.rb', line 249 def open_tag_def tokens << [:OPEN_TAG_DEF] stack.push(:tag, :tag_def) end |
#potential_expression_inner_tag ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/rbexy/lexer.rb', line 239 def potential_expression_inner_tag if self.curr_expr =~ Patterns.expression_internal_tag_prefixes tokens << [:EXPRESSION_BODY, curr_expr] self.curr_expr = "" open_tag_def else self.curr_expr += scanner.matched end end |
#tokenize ⇒ Object
51 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rbexy/lexer.rb', line 51 def tokenize until scanner.eos? case stack.last when :default if scanner.scan(Patterns.declaration) tokens << [:DECLARATION, scanner.matched] elsif scanner.scan(Patterns.open_tag_def) open_tag_def elsif scanner.scan(Patterns.open_expression) open_expression elsif scanner.scan(Patterns.comment) tokens << [:SILENT_NEWLINE] elsif scanner.check(Patterns.text_content) stack.push(:default_text) else raise SyntaxError, self end when :tag if scanner.scan(Patterns.open_tag_def) open_tag_def elsif scanner.scan(Patterns.open_tag_end) tokens << [:OPEN_TAG_END] stack.push(:tag_end) elsif scanner.scan(Patterns.open_expression) open_expression elsif scanner.scan(Patterns.comment) tokens << [:SILENT_NEWLINE] elsif scanner.check(Patterns.text_content) stack.push(:default_text) else raise SyntaxError, self end when :default_text if scanner.scan(Patterns.text_content) self.curr_default_text += scanner.matched if scanner.matched.end_with?('\\') && scanner.peek(1) == "{" self.curr_default_text += scanner.getch elsif scanner.matched.end_with?('\\') && scanner.peek(1) == "#" self.curr_default_text += scanner.getch else if scanner.peek(1) == "#" # If the next token is a comment, trim trailing whitespace from # the text value so we don't add to the indentation of the next # value that is output after the comment self.curr_default_text = curr_default_text.gsub(/^\p{Blank}*\z/, "") end tokens << [:TEXT, curr_default_text] self.curr_default_text = "" stack.pop end else raise SyntaxError, self end when :expression if scanner.scan(Patterns.close_expression) tokens << [:EXPRESSION_BODY, curr_expr] tokens << [:CLOSE_EXPRESSION] self.curr_expr = "" stack.pop elsif scanner.scan(Patterns.open_expression) expression_inner_bracket elsif scanner.scan(Patterns.double_quote) expression_inner_double_quote elsif scanner.scan(Patterns.single_quote) expression_inner_single_quote elsif scanner.scan(Patterns.open_tag_def) potential_expression_inner_tag elsif expression_content? self.curr_expr += scanner.matched else raise SyntaxError, self end when :expression_inner_bracket if scanner.scan(Patterns.close_expression) self.curr_expr += scanner.matched stack.pop elsif scanner.scan(Patterns.open_expression) expression_inner_bracket elsif scanner.scan(Patterns.double_quote) expression_inner_double_quote elsif scanner.scan(Patterns.single_quote) expression_inner_single_quote elsif scanner.scan(Patterns.open_tag_def) potential_expression_inner_tag elsif expression_content? self.curr_expr += scanner.matched else raise SyntaxError, self end when :expression_inner_double_quote if scanner.check(Patterns.double_quote) expression_quoted_string_content elsif scanner.scan(Patterns.double_quoted_text_content) self.curr_expr += scanner.matched else raise SyntaxError, self end when :expression_inner_single_quote if scanner.check(Patterns.single_quote) expression_quoted_string_content elsif scanner.scan(Patterns.single_quoted_text_content) self.curr_expr += scanner.matched else raise SyntaxError, self end when :tag_def if scanner.scan(Patterns.close_self_closing_tag) tokens << [:CLOSE_TAG_DEF] tokens << [:OPEN_TAG_END] tokens << [:CLOSE_TAG_END] stack.pop(2) elsif scanner.scan(Patterns.close_tag) tokens << [:CLOSE_TAG_DEF] stack.pop elsif scanner.scan(Patterns.tag_name) tokens << [:TAG_NAME, scanner.matched] elsif scanner.scan(Patterns.whitespace) scanner.matched.count("\n").times { tokens << [:SILENT_NEWLINE] } tokens << [:OPEN_ATTRS] stack.push(:tag_attrs) else raise SyntaxError, self end when :tag_end if scanner.scan(Patterns.close_tag) tokens << [:CLOSE_TAG_END] stack.pop(2) elsif scanner.scan(Patterns.tag_name) tokens << [:TAG_NAME, scanner.matched] else raise SyntaxError, self end when :tag_attrs if scanner.scan(Patterns.whitespace) scanner.matched.count("\n").times { tokens << [:SILENT_NEWLINE] } elsif scanner.check(Patterns.close_tag) tokens << [:CLOSE_ATTRS] stack.pop elsif scanner.scan(Patterns.attr_assignment) tokens << [:OPEN_ATTR_VALUE] stack.push(:tag_attr_value) elsif scanner.scan(Patterns.attr) tokens << [:ATTR_NAME, scanner.matched.strip] elsif scanner.scan(Patterns.open_attr_splat) tokens << [:OPEN_ATTR_SPLAT] tokens << [:OPEN_EXPRESSION] stack.push(:tag_attr_splat, :expression) else raise SyntaxError, self end when :tag_attr_value if scanner.scan(Patterns.double_quote) stack.push(:quoted_text) elsif scanner.scan(Patterns.open_expression) open_expression elsif scanner.scan(Patterns.whitespace) || scanner.check(Patterns.close_tag) tokens << [:CLOSE_ATTR_VALUE] scanner.matched.count("\n").times { tokens << [:SILENT_NEWLINE] } stack.pop else raise SyntaxError, self end when :tag_attr_splat # Splat is consumed by :expression. It pops control back to here once # it's done, and we just record the completion and pop back to :tag_attrs tokens << [:CLOSE_ATTR_SPLAT] stack.pop when :quoted_text if scanner.scan(Patterns.double_quoted_text_content) self.curr_quoted_text += scanner.matched if scanner.matched.end_with?('\\') && scanner.peek(1) == "\"" self.curr_quoted_text += scanner.getch end elsif scanner.scan(Patterns.double_quote) tokens << [:TEXT, curr_quoted_text] self.curr_quoted_text = "" stack.pop else raise SyntaxError, self end else raise SyntaxError, self end end tokens end |