Initial commit

edge_only_predecessors
Max Brunsfeld 2014-09-05 23:52:10 +07:00
parent af2904ca83
commit c91415338d
6 changed files with 204 additions and 0 deletions

9
.gitignore vendored

@ -0,0 +1,9 @@
node_modules
build
# files generated by node-tree-sitter-compiler
index.js
binding.gyp
grammar.json
src
include

@ -0,0 +1,3 @@
grammar_test
build
script

@ -0,0 +1,85 @@
compiler = require("tree-sitter-compiler")
{ choice, err, repeat, seq, keyword, optional, token } = compiler.rules
commaSep = (rule) ->
optional(commaSep1(rule))
commaSep1 = (rule) ->
seq(rule, repeat(seq(",", rule)))
module.exports = compiler.grammar
name: 'c',
separators: [' ', '\t', '\r', '\n']
ubiquitous: ["comment"]
rules:
program: -> repeat(choice(
@preproc_call,
@typedef,
@var_declaration))
preproc_call: -> choice(
@preproc_include,
@preproc_define,
seq(@preproc_directive, /.*/)
)
preproc_include: -> seq(
keyword("#include"),
choice(@string, @system_lib_string))
preproc_define: -> seq(
keyword("#define"),
@identifier,
/.+/)
preproc_directive: -> /#\a\w+/
typedef: -> seq(
keyword("typedef"),
@type,
@identifier,
";")
var_declaration: -> seq(
repeat(@type_modifier),
@identifier,
commaSep1(@type_expression),
";")
type_expression: -> choice(
@identifier,
@pointer_type)
type: -> choice(
@identifier,
@struct_type)
pointer_type: -> seq(
"*", @type_expression)
struct_type: -> seq(
keyword("struct"),
"{", err(repeat(seq(@field, ";"))), "}")
field: -> seq(@type, @type_expression)
type_modifier: -> choice(
keyword("const"),
keyword("static"),
keyword("inline"),
keyword("extern"))
string: -> token(seq(
'"', repeat(choice(/[^"]/, '\\"')), '"'))
system_lib_string: -> token(seq(
'<', repeat(choice(/[^>]/, '\\>')), '>'))
identifier: -> /\a\w*/
comment: -> token(choice(
seq("//", /.*/),
seq("/*", repeat(choice(/[^\*]/, /\*[^/]/)), "*/")))

@ -0,0 +1,61 @@
============================================
Simple variable declarations
============================================
size_t the_size;
---
(program
(var_declaration (identifier) (identifier)))
============================================
Pointer variable declarations
============================================
char *the_string;
const char **the_string, **the_other_string;
---
(program
(var_declaration (identifier) (pointer_type (identifier)))
(var_declaration (type_modifier) (identifier)
(pointer_type (pointer_type (identifier)))
(pointer_type (pointer_type (identifier)))))
============================================
Simple typedefs
============================================
typedef int my_int;
---
(program
(typedef (identifier) (identifier)))
============================================
Struct typedefs
============================================
typedef struct {
size_t i;
struct {
float x;
float y;
} pos;
} my_struct;
---
(program (typedef
(struct_type
(field (identifier) (identifier))
(field
(struct_type
(field (identifier) (identifier))
(field (identifier) (identifier)))
(identifier)))
(identifier)))

@ -0,0 +1,23 @@
============================================
Include directives
============================================
#include "some/path.h"
#include <stdint.h>
---
(program
(preproc_include (string))
(preproc_include (system_lib_string)))
============================================
Defines
============================================
#define SIZE 1024
---
(program
(preproc_define (identifier)))

@ -0,0 +1,23 @@
{
"name": "tree-sitter-c",
"version": "0.0.0",
"description": "C grammar for node-tree-sitter",
"main": "index.js",
"keywords": [
"parser",
"lexer"
],
"author": "Max Brunsfeld",
"license": "MIT",
"dependencies": {
"bindings": "1.2.x",
"nan": "1.3.x"
},
"devDependencies": {
"tree-sitter-compiler": "*"
},
"scripts": {
"prepublish": "tree-sitter compile",
"test": "tree-sitter test"
}
}