/
coco.co
51 lines (40 loc) · 1.51 KB
/
coco.co
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# The main entry functions for
# [tokenizing](#lexer), [parsing](#grammar), and [compiling](#ast)
# Coco source into JavaScript.
lexer = require \./lexer
# Override Jison's default lexer, so that it can accept
# the generic stream of tokens our lexer produces.
{parser} = require \./parser
parser import
yy : require \./ast
lexer :
lex : -> [tag, @yytext, @yylineno] = @tokens[++@pos] or ['']; tag
setInput : -> @pos = -1; @tokens = it
upcomingInput : -> ''
exports import
VERSION: \0.7.2
# Compiles a string of Coco code to JavaScript.
compile: (code, options) ->
try parser.parse(lexer.lex code)compileRoot options catch
e.message += "\nat #that" if options?filename
throw e
# Parses a string or tokens of Coco code,
# returning the [AST](http://en.wikipedia.org/wiki/Abstract_syntax_tree).
ast: -> parser.parse if typeof it is \string then lexer.lex it else it
# Tokenizes a string of Coco code, returning the array of tokens.
tokens: lexer.lex
# Same as `tokens`, except that this skips rewriting.
lex: -> lexer.lex it, {+raw}
# Runs Coco code directly.
run: (code, options) -> do Function exports.compile code, {...options, +bare}
exports.tokens{rewrite} = lexer
# Export AST constructors.
exports.ast import all parser.yy
if require.extensions
# -> [node](#node)
require(\./node) exports
else
# Attach `require` for debugging.
exports import {require}
# Support Gecko JS Module.
@EXPORTED_SYMBOLS = [\Coco] if ''+this is '[object BackstagePass]'