Initial commit

Co-Authored-By: Ashi Krishan <queerviolet@github.com>
This commit is contained in:
Max Brunsfeld 2018-06-11 13:18:00 -07:00
commit e74d96c27b
14 changed files with 2033 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
/src/** linguist-vendored

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
build
*.log

3
.npmignore Normal file
View File

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

14
.travis.yml Normal file
View File

@ -0,0 +1,14 @@
language: node_js
sudo: false
node_js:
- "node"
compiler: clang-3.6
env:
- CXX=clang-3.6
addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- ubuntu-toolchain-r-test
packages:
- clang-3.6

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Max Brunsfeld
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

18
binding.gyp Normal file
View File

@ -0,0 +1,18 @@
{
"targets": [
{
"target_name": "tree_sitter_html_binding",
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
],
"sources": [
"src/parser.c",
"src/binding.cc"
],
"cflags_c": [
"-std=c99",
]
}
]
}

78
corpus/main.txt Normal file
View File

@ -0,0 +1,78 @@
===================================
Tags
===================================
<span>Hello</span>
---
(fragment
(element
(start_tag (tag_name))
(text)
(end_tag (tag_name))))
===================================
Tags with attributes
===================================
<input value=yes class="a" data-💩></input>
---
(fragment
(void_element
(void_start_tag
(void_tag_name)
(attribute
(attribute_name)
(attribute_value))
(attribute
(attribute_name)
(quoted_attribute_value (attribute_value)))
(attribute
(attribute_name)))
(end_tag (tag_name))))
===================================
Nested tags
===================================
<div>
<span>a</span>
b
<b>c</b>
</div>
---
(fragment
(element
(start_tag (tag_name))
(text)
(element
(start_tag (tag_name))
(text)
(end_tag (tag_name)))
(text)
(element
(start_tag (tag_name))
(text)
(end_tag (tag_name)))
(text)
(end_tag (tag_name))))
==================================
Void tags
==================================
<form><img src="somethign.png"><br><input type=submit value=Ok /></form>
---
(fragment
(element
(start_tag (tag_name))
(void_element
(void_start_tag
(tag_name)
(attribute (attribute_name) (quoted_attribute_value (attribute_value)))))
(void_element (void_start_tag (tag_name)))
(void_element
(self_closing_tag
(tag_name)
(attribute (attribute_name) (attribute_value))
(attribute (attribute_name) (attribute_value))))
(end_tag (tag_name))))

102
grammar.js Normal file
View File

@ -0,0 +1,102 @@
const startTag = ($, tag) => seq(
'<',
alias(tag, $.tag_name),
repeat($.attribute),
'>'
)
module.exports = grammar({
name: 'html',
externals: $ => [
$.tag_name,
],
rules: {
fragment: $ => repeat($._node),
_node: $ => choice(
$.text,
$.element,
$.void_element
),
element: $ => seq(
$.start_tag,
repeat($._node),
$.end_tag
),
void_element: $ => choice(
seq($.void_start_tag, optional($.end_tag)),
$.self_closing_tag
),
start_tag: $ => startTag($, $.tag_name),
void_start_tag: $ => startTag($, $.void_tag_name),
self_closing_tag: $ => seq(
'<',
choice($.tag_name, $.void_tag_name),
repeat($.attribute),
'/',
'>'
),
attribute: $ => seq(
alias($._attribute_part, $.attribute_name),
optional(seq(
'=',
choice(
alias($._attribute_part, $.attribute_value),
$.quoted_attribute_value
)
))
),
_attribute_part: $ => /[^<>"'/=\s]+/,
quoted_attribute_value: $ => choice(
seq("'", optional(alias(/[^']+/, $.attribute_value)), "'"),
seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"')
),
end_tag: $ => seq(
'</',
choice($.tag_name, $.void_tag_name),
'>'
),
tag_name: $ => /[a-zA-Z\-]+/,
void_tag_name: $ => token(prec(1, choice(
'area',
'base',
'basefont',
'bgsound',
'br',
'col',
'command',
'embed',
'frame',
'hr',
'image',
'img',
'input',
'isindex',
'keygen',
'link',
'menuitem',
'meta',
'nextid',
'param',
'source',
'track',
'wbr'
))),
text: $ => /[^<>]+/
}
});

9
index.js Normal file
View File

@ -0,0 +1,9 @@
try {
module.exports = require("./build/Release/tree_sitter_html_binding");
} catch (error) {
try {
module.exports = require("./build/Debug/tree_sitter_html_binding");
} catch (_) {
throw error
}
}

28
src/binding.cc vendored Normal file
View File

@ -0,0 +1,28 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
using namespace v8;
extern "C" TSLanguage * tree_sitter_html();
namespace {
NAN_METHOD(New) {}
void Init(Handle<Object> exports, Handle<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Local<Function> constructor = tpl->GetFunction();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_html());
instance->Set(Nan::New("name").ToLocalChecked(), Nan::New("html").ToLocalChecked());
module->Set(Nan::New("exports").ToLocalChecked(), instance);
}
NODE_MODULE(tree_sitter_html_binding, Init)
} // namespace

417
src/grammar.json vendored Normal file
View File

@ -0,0 +1,417 @@
{
"name": "html",
"rules": {
"fragment": {
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_node"
}
},
"_node": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "text"
},
{
"type": "SYMBOL",
"name": "element"
},
{
"type": "SYMBOL",
"name": "void_element"
}
]
},
"element": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "start_tag"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_node"
}
},
{
"type": "SYMBOL",
"name": "end_tag"
}
]
},
"void_element": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "void_start_tag"
},
{
"type": "SYMBOL",
"name": "self_closing_tag"
}
]
},
"start_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "tag_name"
},
"named": true,
"value": "tag_name"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "attribute"
}
},
{
"type": "STRING",
"value": ">"
}
]
},
"void_start_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "void_tag_name"
},
"named": true,
"value": "tag_name"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "attribute"
}
},
{
"type": "STRING",
"value": ">"
}
]
},
"self_closing_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "tag_name"
},
{
"type": "SYMBOL",
"name": "void_tag_name"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "attribute"
}
},
{
"type": "STRING",
"value": "/"
},
{
"type": "STRING",
"value": ">"
}
]
},
"attribute": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attribute_part"
},
"named": true,
"value": "attribute_name"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_attribute_part"
},
"named": true,
"value": "attribute_value"
},
{
"type": "SYMBOL",
"name": "quoted_attribute_value"
}
]
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"_attribute_part": {
"type": "PATTERN",
"value": "[^<>\"'\\/=\\s]+"
},
"quoted_attribute_value": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[^']+"
},
"named": true,
"value": "attribute_value"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "'"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "[^\"]+"
},
"named": true,
"value": "attribute_value"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "\""
}
]
}
]
},
"end_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "STRING",
"value": "/"
},
{
"type": "SYMBOL",
"name": "tag_name"
},
{
"type": "STRING",
"value": ">"
}
]
},
"tag_name": {
"type": "PATTERN",
"value": "[a-zA-Z\\-]+"
},
"void_tag_name": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "area"
},
{
"type": "STRING",
"value": "base"
},
{
"type": "STRING",
"value": "basefont"
},
{
"type": "STRING",
"value": "bgsound"
},
{
"type": "STRING",
"value": "br"
},
{
"type": "STRING",
"value": "col"
},
{
"type": "STRING",
"value": "command"
},
{
"type": "STRING",
"value": "embed"
},
{
"type": "STRING",
"value": "frame"
},
{
"type": "STRING",
"value": "hr"
},
{
"type": "STRING",
"value": "image"
},
{
"type": "STRING",
"value": "img"
},
{
"type": "STRING",
"value": "input"
},
{
"type": "STRING",
"value": "isindex"
},
{
"type": "STRING",
"value": "keygen"
},
{
"type": "STRING",
"value": "link"
},
{
"type": "STRING",
"value": "menuitem"
},
{
"type": "STRING",
"value": "meta"
},
{
"type": "STRING",
"value": "nextid"
},
{
"type": "STRING",
"value": "param"
},
{
"type": "STRING",
"value": "source"
},
{
"type": "STRING",
"value": "track"
},
{
"type": "STRING",
"value": "wbr"
}
]
}
}
},
"text": {
"type": "PATTERN",
"value": "[^<>]+"
}
},
"extras": [
{
"type": "PATTERN",
"value": "\\s"
}
],
"conflicts": [],
"externals": [],
"inline": []
}

1146
src/parser.c vendored Normal file

File diff suppressed because it is too large Load Diff

192
src/tree_sitter/parser.h vendored Normal file
View File

@ -0,0 +1,192 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_RUNTIME_H_
typedef uint16_t TSSymbol;
typedef struct TSLanguage TSLanguage;
#endif
typedef uint16_t TSStateId;
typedef struct {
bool visible : 1;
bool named : 1;
} TSSymbolMetadata;
typedef struct {
void (*advance)(void *, bool);
void (*mark_end)(void *);
uint32_t (*get_column)(void *);
int32_t lookahead;
TSSymbol result_symbol;
} TSLexer;
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef struct {
union {
struct {
TSStateId state;
bool extra : 1;
bool repetition : 1;
};
struct {
TSSymbol symbol;
int16_t dynamic_precedence;
uint8_t child_count;
uint8_t alias_sequence_id;
};
} params;
TSParseActionType type : 4;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable : 1;
};
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
const char **symbol_names;
const TSSymbolMetadata *symbol_metadata;
const uint16_t *parse_table;
const TSParseActionEntry *parse_actions;
const TSLexMode *lex_modes;
const TSSymbol *alias_sequences;
uint16_t max_alias_sequence_length;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)();
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
};
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
int32_t lookahead; \
next_state: \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
lexer->advance(lexer, false); \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
lexer->advance(lexer, true); \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{ \
{ \
.type = TSParseActionTypeShift, \
.params = {.state = state_value}, \
} \
}
#define SHIFT_REPEAT(state_value) \
{ \
{ \
.type = TSParseActionTypeShift, \
.params = { \
.state = state_value, \
.repetition = true \
}, \
} \
}
#define RECOVER() \
{ \
{ .type = TSParseActionTypeRecover } \
}
#define SHIFT_EXTRA() \
{ \
{ \
.type = TSParseActionTypeShift, \
.params = {.extra = true} \
} \
}
#define REDUCE(symbol_val, child_count_val, ...) \
{ \
{ \
.type = TSParseActionTypeReduce, \
.params = { \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
} \
} \
}
#define ACCEPT_INPUT() \
{ \
{ .type = TSParseActionTypeAccept } \
}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_

1
test.html Normal file
View File

@ -0,0 +1 @@
<input value=stuff></input>