Add automatic semicolon insertion

pull/70/head
Max Brunsfeld 2014-08-04 13:34:25 +07:00
parent 64f45fe2b7
commit 6a6c8f067b
2 changed files with 26 additions and 5 deletions

@ -1,13 +1,16 @@
compiler = require("tree-sitter-compiler")
{ blank, choice, repeat, seq, keyword, token, optional } = compiler.rules
{ blank, choice, repeat, seq, sym, keyword, token, optional } = compiler.rules
commaSep = (rule) ->
choice(blank(), seq(rule, repeat(seq(",", rule))))
terminator = ->
choice(";", sym("_line_break"))
module.exports = compiler.grammar
name: 'javascript',
ubiquitous: ["comment"]
ubiquitous: ["comment", "_line_break"]
rules:
program: -> repeat(@statement)
@ -21,7 +24,7 @@ module.exports = compiler.grammar
@var_declaration)
expression_statement: -> seq(
optional(@expression), ";")
optional(@expression), terminator())
if_statement: -> seq(
keyword("if"),
@ -40,7 +43,7 @@ module.exports = compiler.grammar
return_statement: -> seq(
keyword("return"),
optional(@expression),
";")
terminator())
var_declaration: -> seq(
keyword("var"),
@ -50,7 +53,7 @@ module.exports = compiler.grammar
@identifier,
"=",
@expression))),
";")
terminator())
expression: -> choice(
@identifier,
@ -164,3 +167,5 @@ module.exports = compiler.grammar
false: -> keyword("false")
null: -> keyword("null")
undefined: -> keyword("undefined")
_line_break: -> "\n"

@ -102,3 +102,19 @@ Comments
(identifier) (number)
(comment)
(identifier) (function (formal_parameters) (statement_block)))))
============================================
Automatic semicolon insertion
============================================
stuff()
return 5
var x = y
---
(program
(expression_statement (function_call (identifier)))
(return_statement (number))
(var_declaration (identifier) (identifier)))