Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add line offsets to parser ast

  • Loading branch information...
commit cc9c5184cfcaf236e385b28a0cd61542b4b94c53 1 parent 228bea9
Joshua Peek josh authored
10 lib/mustache/generator.rb
View
@@ -90,7 +90,7 @@ def compile!(exp)
# Callback fired when the compiler finds a section token. We're
# passed the section name and the array of tokens.
- def on_section(name, content, raw, delims)
+ def on_section(name, offset, content, raw, delims)
# Convert the tokenized content of this section into a Ruby
# string we can use.
code = compile(content)
@@ -121,7 +121,7 @@ def t.tokens(src=@source)
# Fired when we find an inverted section. Just like `on_section`,
# we're passed the inverted section name and the array of tokens.
- def on_inverted_section(name, content, raw, _)
+ def on_inverted_section(name, offset, content, raw, delims)
# Convert the tokenized content of this section into a Ruby
# string we can use.
code = compile(content)
@@ -139,12 +139,12 @@ def on_inverted_section(name, content, raw, _)
# Fired when the compiler finds a partial. We want to return code
# which calls a partial at runtime instead of expanding and
# including the partial's body to allow for recursive partials.
- def on_partial(name, indentation)
+ def on_partial(name, offset, indentation)
ev("ctx.partial(#{name.to_sym.inspect}, #{indentation.inspect})")
end
# An unescaped tag.
- def on_utag(name)
+ def on_utag(name, offset)
ev(<<-compiled)
v = #{compile!(name)}
if v.is_a?(Proc)
@@ -155,7 +155,7 @@ def on_utag(name)
end
# An escaped tag.
- def on_etag(name)
+ def on_etag(name, offset)
ev(<<-compiled)
v = #{compile!(name)}
if v.is_a?(Proc)
14 lib/mustache/parser.rb
View
@@ -146,12 +146,12 @@ def scan_tags
case type
when '#'
block = [:multi]
- @result << [:mustache, :section, fetch, block]
+ @result << [:mustache, :section, fetch, offset, block]
@sections << [content, position, @result]
@result = block
when '^'
block = [:multi]
- @result << [:mustache, :inverted_section, fetch, block]
+ @result << [:mustache, :inverted_section, fetch, offset, block]
@sections << [content, position, @result]
@result = block
when '/'
@@ -169,14 +169,14 @@ def scan_tags
when '='
self.otag, self.ctag = content.split(' ', 2)
when '>', '<'
- @result << [:mustache, :partial, content, padding]
+ @result << [:mustache, :partial, content, offset, padding]
when '{', '&'
# The closing } in unescaped tags is just a hack for
# aesthetics.
type = "}" if type == "{"
- @result << [:mustache, :utag, fetch]
+ @result << [:mustache, :utag, fetch, offset]
else
- @result << [:mustache, :etag, fetch]
+ @result << [:mustache, :etag, fetch, offset]
end
# Skip whitespace and any balancing sigils after the content
@@ -234,6 +234,10 @@ def scan_until_exclusive(regexp)
end
end
+ def offset
+ position[0, 2]
+ end
+
# Returns [lineno, column, line]
def position
# The rest of the current line
13 test/parser_test.rb
View
@@ -22,29 +22,32 @@ def test_parser
expected = [:multi,
[:static, "<h1>"],
- [:mustache, :etag, [:mustache, :fetch, ["header"]]],
+ [:mustache, :etag, [:mustache, :fetch, ["header"]], [1, 11]],
[:static, "</h1>\n"],
[:mustache,
:section,
[:mustache, :fetch, ["items"]],
+ [2, 7],
[:multi,
[:mustache,
:section,
[:mustache, :fetch, ["first"]],
+ [3, 7],
[:multi,
[:static, " <li><strong>"],
- [:mustache, :etag, [:mustache, :fetch, ["name"]]],
+ [:mustache, :etag, [:mustache, :fetch, ["name"]], [4, 19]],
[:static, "</strong></li>\n"]],
%Q' <li><strong>{{name}}</strong></li>\n',
%w[{{ }}]],
[:mustache,
:section,
[:mustache, :fetch, ["link"]],
+ [6, 6],
[:multi,
[:static, " <li><a href=\""],
- [:mustache, :etag, [:mustache, :fetch, ["url"]]],
+ [:mustache, :etag, [:mustache, :fetch, ["url"]], [7, 19]],
[:static, "\">"],
- [:mustache, :etag, [:mustache, :fetch, ["name"]]],
+ [:mustache, :etag, [:mustache, :fetch, ["name"]], [7, 29]],
[:static, "</a></li>\n"]],
%Q' <li><a href="{{url}}">{{name}}</a></li>\n',
%w[{{ }}]]],
@@ -54,6 +57,7 @@ def test_parser
[:mustache,
:section,
[:mustache, :fetch, ["empty"]],
+ [11, 7],
[:multi, [:static, "<p>The list is empty.</p>\n"]],
%Q'<p>The list is empty.</p>\n',
%w[{{ }}]]]
@@ -69,6 +73,7 @@ def test_raw_content_and_whitespace
[:mustache,
:section,
[:mustache, :fetch, ["list"]],
+ [1, 6],
[:multi, [:static, "\t"]],
"\t",
%w[{{ }}]]]
Please sign in to comment.
Something went wrong with that request. Please try again.