shrub/web/lib/js/hoon.js

165 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-05-20 06:29:04 +03:00
CodeMirror.defineMode("hoon", function() {
glyph = /[+\-|$%:.#^~;=?!_,&\/<>%*]/
term = /^[$&|]|^[a-z]([a-z0-9\-]*[a-z0-9])?/
2016-02-11 03:38:47 +03:00
num = /~[a-z0-9._~-]+|-?-?^[0-9]([0-9.]*|[xwbv]?[0-9a-zA-Z.-~]*)/
2015-05-20 06:29:04 +03:00
res = {}
2016-02-11 03:38:47 +03:00
res.startState = function(){return {soblock: false, doqblock:false, sail:false, space:true}}
2015-05-20 06:29:04 +03:00
var propOrVar = function(c){
if(c == '.')
return 'property'
return 'variable'
}
res.token = function(stream, state){
if(state.soqblock && stream.sol()){
if(stream.match(/\s*'''/)){
state.soqblock = false
}
else {
stream.skipToEnd()
}
return "string"
}
if(state.doqblock){
if(stream.match(/\s*"""/)){
state.doqblock = false
}
else {
stream.skipToEnd()
}
return "string"
}
if(stream.sol())
state.space = true
if(state.sail){
if(stream.peek().match(/[^#./() ]/)||stream.eol()){
state.sail = false
if(stream.match(/:? /)){
stream.skipToEnd()
return 'string'
}
if(stream.match(term))
state.sail = true
return;
if(stream.match(':'))
state.sail = true
return 'operator'
}
}
if(stream.match("'")){
if(stream.match("''")){
state.soqblock = true
return 'string'
}
while(stream.match(/^[^'\\]/) || stream.match(/\\./));
stream.eat("'")
return 'string'
}
if(stream.match('"')){
if(stream.match('""')){
state.doqblock = true
stream.skipToEnd()
return 'string'
}
2016-02-11 03:38:47 +03:00
while(stream.match(/^[^"\\]/) || stream.match(/\\./));
stream.eat('"')
return 'string'
2015-05-20 06:29:04 +03:00
}
if(stream.match(' ;')){
if(stream.eat(' ')){
stream.skipToEnd()
return 'string'
}
if(!stream.match(glyph)){
state.sail = true
}
return 'builtin'
}
if(stream.match('::')){
stream.skipToEnd()
return 'comment'
}
if(stream.match('++ ') || stream.match('+- ')){
stream.match(term)
return 'header'
}
if(state.space && stream.match('--')){
if(stream.eat(glyph) || stream.eat(/[a-z0-9]/))
stream.backUp(3)
else return 'header'
}
2016-02-11 03:38:47 +03:00
if(stream.match(/^@[a-z]*[A-Z]?/))
return 'atom'
if(stream.match(num))
return 'number'
if(stream.eat(/[%$]/))
if(stream.match(term) || stream.match(num) || stream.match('~'))
2015-05-20 06:29:04 +03:00
return 'tag'
else stream.backUp(1)
if(state.space && stream.match('==')){
return 'tag'
}
2016-02-11 03:38:47 +03:00
if(stream.eat('~')){
if(/[()]/.exec(stream.peek()))
return 'builtin'
return 'tag'
}
2015-05-20 06:29:04 +03:00
if(stream.eat(/[+\-]/)){
while(stream.eat(/[<>]/) && stream.eat(/[+\-]/));
return propOrVar(stream.peek())
}
if(stream.eat('`')){
2016-02-11 03:38:47 +03:00
state.space = false
2015-05-20 06:29:04 +03:00
return 'operator'
}
if(stream.sol() && stream.eatWhile(glyph)){
state.space = false
return 'builtin'
}
if(stream.eat(glyph)){
state.space = false
stream.backUp(2)
if(stream.eat(/[ ([{]/) || (stream.peek().match(/[^+\-<>]/)
&& stream.eat(glyph))){ // expression start
stream.eatWhile(glyph)
return 'builtin'
}
stream.next()
if(state.space && stream.eat('=')){
if(/[()]/.exec(stream.peek()))
return 'builtin'
return 'operator'
}
2016-02-11 03:38:47 +03:00
if(stream.eat(/[=:.^/]/))
2015-05-20 06:29:04 +03:00
return 'operator'
stream.next()
return 'builtin'
}
if(stream.match(term)){
2016-02-11 03:38:47 +03:00
if(state.space && stream.match('+'))
2015-05-20 06:29:04 +03:00
return 'tag'
state.space = false
return propOrVar(stream.peek())
}
2016-02-11 03:38:47 +03:00
if(stream.eat(/[ \[({]/)){
2015-05-20 06:29:04 +03:00
state.space = true
return
}
stream.next()
}
res.lineComment = '::'
res.fold = "indent"
return res
});
CodeMirror.defineMIME("text/x-hoon", "hoon");