diff --git a/spec/app/parser-spec.coffee b/spec/app/parser-spec.coffee index a7ee6f6f0..9da526da3 100644 --- a/spec/app/parser-spec.coffee +++ b/spec/app/parser-spec.coffee @@ -77,14 +77,4 @@ fdescribe "Parser", -> expect(tokens[0]).toEqual value: '"""', scopes: ['source.coffee', 'string.quoted.double.heredoc.coffee', 'punctuation.definition.string.begin.coffee'] expect(tokens[1]).toEqual value: "heredoc with character escape ", scopes: ['source.coffee', 'string.quoted.double.heredoc.coffee'] expect(tokens[2]).toEqual value: "\\t", scopes: ['source.coffee', 'string.quoted.double.heredoc.coffee', 'constant.character.escape.coffee'] - expect(tokens[3]).toEqual value: '"""', scopes: ['source.coffee', 'string.quoted.double.heredoc.coffee', 'punctuation.definition.string.end.coffee'] - - describe "when the line matches a pattern that includes a rule", -> - it "returns tokens based on the included rule", -> - {tokens} = parser.getLineTokens("7777777") - - expect(tokens.length).toBe 1 - - expect(tokens[0]).toEqual value: '7777777', scopes: ['source.coffee', 'constant.numeric.coffee'] - - + expect(tokens[3]).toEqual value: '"""', scopes: ['source.coffee', 'string.quoted.double.heredoc.coffee', 'punctuation.definition.string.end.coffee'] \ No newline at end of file diff --git a/src/app/parser.coffee b/src/app/parser.coffee index 546073d77..21088b87b 100644 --- a/src/app/parser.coffee +++ b/src/app/parser.coffee @@ -36,17 +36,16 @@ class Parser class Grammar initialRule: null - constructor: ({ scopeName, patterns, @repository }) -> - @initialRule = new Rule(this, {scopeName, patterns}) + constructor: ({ scopeName, patterns }) -> + @initialRule = new Rule({scopeName, patterns}) class Rule - grammar: null parentRule: null scopeName: null patterns: null endPattern: null - constructor: (@grammar, {@parentRule, @scopeName, patterns, @endPattern}) -> + constructor: ({@parentRule, @scopeName, patterns, @endPattern}) -> patterns ?= [] @patterns = patterns.map (pattern) => new Pattern(this, pattern) @patterns.push(@endPattern) if @endPattern @@ -66,17 +65,15 @@ class Rule nextMatch = null matchedPattern = null for pattern in @patterns - { match, pattern } = pattern.getNextMatch(line, position) - if match + continue unless pattern.regex # TODO: we should eventually not need this + if match = pattern.regex.search(line, position) if !nextMatch or match.index < nextMatch.index nextMatch = match matchedPattern = pattern { match: nextMatch, pattern: matchedPattern } getScopes: -> - scopes = @parentRule?.getScopes() ? [] - scopes = scopes.concat(@scopeName) if @scopeName - scopes + (@parentRule?.getScopes() ? []).concat(@scopeName) class Pattern parentRule: null @@ -85,27 +82,17 @@ class Pattern regex: null 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}) - - getNextMatch: (line, position) -> - if @includeRule - @includeRule.getNextMatch(line, position) - else - {pattern: this, match: @regex?.search(line, position)} + constructor: (@parentRule, { name, match, begin, end, captures, beginCaptures, endCaptures, patterns }) -> + @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, @scopeName, patterns, endPattern}) getTokensForMatch: (match) -> tokens = []