$self includes work

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-08-01 11:37:44 -07:00
parent bccd525084
commit 487c4ed439
2 changed files with 19 additions and 2 deletions

View File

@@ -84,3 +84,15 @@ fdescribe "Parser", ->
{tokens} = parser.getLineTokens("7777777")
expect(tokens.length).toBe 1
expect(tokens[0]).toEqual value: '7777777', scopes: ['source.coffee', 'constant.numeric.coffee']
describe "when the line is an interpolated string", ->
it "returns the correct tokens", ->
{tokens} = parser.getLineTokens('"the value is #{@x} my friend"')
expect(tokens[0]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.begin.coffee"]
expect(tokens[1]).toEqual value: "the value is ", scopes: ["source.coffee","string.quoted.double.coffee"]
expect(tokens[2]).toEqual value: '#{', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"]
expect(tokens[3]).toEqual value: "@x", scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","variable.other.readwrite.instance.coffee"]
expect(tokens[4]).toEqual value: "}", scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"]
expect(tokens[5]).toEqual value: " my friend", scopes: ["source.coffee","string.quoted.double.coffee"]
expect(tokens[6]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.end.coffee"]

View File

@@ -47,6 +47,8 @@ class Grammar
ruleForInclude: (name) ->
if name[0] == "#"
@repository[name[1..]]
else if name == "$self"
@initialRule
class Rule
grammar: null
@@ -56,8 +58,9 @@ class Rule
constructor: (@grammar, {@scopeName, patterns, @endPattern}) ->
patterns ?= []
@patterns = patterns.map (pattern) => new Pattern(grammar, pattern)
@patterns = []
@patterns.push(@endPattern) if @endPattern
@patterns.push((patterns.map (pattern) => new Pattern(grammar, pattern))...)
getNextTokens: (stack, line, position) ->
{ match, pattern } = @getNextMatch(line, position)
@@ -73,6 +76,7 @@ class Rule
getNextMatch: (line, position) ->
nextMatch = null
matchedPattern = null
for pattern in @patterns
{ pattern, match } = pattern.getNextMatch(line, position)
if match
@@ -103,7 +107,8 @@ class Pattern
getNextMatch: (line, position) ->
if @include
@grammar.ruleForInclude(@include).getNextMatch(line, position)
rule = @grammar.ruleForInclude(@include)
rule.getNextMatch(line, position)
else
{ match: @regex.search(line, position), pattern: this }