   captures: null
 
   constructor: (@parentRule, { name, match, begin, end, captures, beginCaptures, endCaptures, patterns, include}) ->
-    @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})
+    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) ->
-    {pattern: this, match: @regex?.search(line, position)}
+    if @includeRule
+      @includeRule.getNextMatch(line, position)
+    else
+      {pattern: this, match: @regex?.search(line, position)}
 
   getTokensForMatch: (match) ->
     tokens = []

    { match: nextMatch, pattern: matchedPattern }

  getScopes: ->
    (@parentRule?.getScopes() ? []).concat(@scopeName)

class Pattern
  parentRule: null


  constructor: (@grammar, {@parentRule, @scopeName, patterns, @endPattern}) ->
    patterns ?= []
    @patterns = patterns.map (pattern) => new Pattern(this, pattern)
    @patterns.push(@endPattern) if @endPattern

  getNextTokens: (line, position) ->
    { match, pattern } = @getNextMatch(line, position)
