Skip to content

Instantly share code, notes, and snippets.

@emirotin
Created December 27, 2014 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emirotin/75077d76541d34e3becc to your computer and use it in GitHub Desktop.
Save emirotin/75077d76541d34e3becc to your computer and use it in GitHub Desktop.
{ Grammar } = require 'first-mate'
scopeName = "source.sbvr"
module.exports =
class LanguageSbvrGrammar extends Grammar
constructor: (registry) ->
name = "SBVR"
super(registry, { name, scopeName, fileTypes: [ 'sbvr' ] })
randScope = ->
scopes = [ 'identifier', 'comment', 'keyword', 'number', 'string', 'regex' ]
return scopes[Math.floor(Math.random() * scopes.length)]
tokenizeLine: (line, ruleStack, firstLine = false) ->
tokens = []
addToken = (text, scope) =>
scope = scopeName + (if scope then ".#{scope}" else '')
tokens.push @registry.createToken(text, [ scope ])
startPos = 0
words = line.split(/\s+/)
for word in words
i = line.indexOf(word, startPos)
if i > startPos
addToken line.substring(startPos, i)
addToken word, randScope()
startPos = i + word.length
if startPos < line.length
addToken line.substring(startPos)
if tokens.length is 0
addToken ""
return { tokens, ruleStack: ruleStack or null }
tokenizeLines: ->
console.log 'Never called'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment