Replace includes with the patterns to which they refer

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-31 17:25:14 -07:00
parent 62c5da760c
commit b56c4dda43

View File

@@ -48,8 +48,16 @@ class Rule
constructor: (@grammar, {@parentRule, @scopeName, patterns, @endPattern}) ->
patterns ?= []
@patterns = patterns.map (pattern) => new Pattern(this, pattern)
@patterns = []
@patterns.push(@endPattern) if @endPattern
for pattern in patterns
if pattern.include
repoName = pattern.include.replace(/^#/, "")
includedPatterns = @grammar.repository[repoName]?.patterns ? []
for includedPattern in includedPatterns
@patterns.push(new Pattern(this, includedPattern))
else
@patterns.push(new Pattern(this, pattern))
getNextTokens: (line, position) ->
{ match, pattern } = @getNextMatch(line, position)
@@ -74,9 +82,7 @@ class Rule
{ match: nextMatch, pattern: matchedPattern }
getScopes: ->
scopes = @parentRule?.getScopes() ? []
scopes = scopes.concat(@scopeName) if @scopeName
scopes
(@parentRule?.getScopes() ? []).concat(@scopeName)
class Pattern
parentRule: null
@@ -86,26 +92,19 @@ class Pattern
captures: null
constructor: (@parentRule, { name, match, begin, end, captures, beginCaptures, endCaptures, patterns, include}) ->
if include
patterns = @parentRule.grammar.repository[include.replace(/^#/, '')]?.patterns
@includeRule = new Rule @parentRule.grammar, {@parentRule, patterns}
else
@scopeName = name
if match
@regex = new OnigRegExp(match)
@captures = captures
@nextRule = @parentRule
else if begin
@regex = new OnigRegExp(begin)
@captures = beginCaptures ? captures
endPattern = new Pattern(@parentRule, { name: @scopeName, match: end, captures: endCaptures ? captures })
@nextRule = new Rule(@parentRule.grammar, {@parentRule, @scopeName, patterns, endPattern})
@scopeName = name
if match
@regex = new OnigRegExp(match)
@captures = captures
@nextRule = @parentRule
else if begin
@regex = new OnigRegExp(begin)
@captures = beginCaptures ? captures
endPattern = new Pattern(@parentRule, { name: @scopeName, match: end, captures: endCaptures ? captures })
@nextRule = new Rule(@parentRule.grammar, {@parentRule, @scopeName, patterns, endPattern})
getNextMatch: (line, position) ->
if @includeRule
@includeRule.getNextMatch(line, position)
else
{pattern: this, match: @regex?.search(line, position)}
{pattern: this, match: @regex?.search(line, position)}
getTokensForMatch: (match) ->
tokens = []