Skip to content

Commit

Permalink
Add fixes to parse aliases and externals with Ruby.
Browse files Browse the repository at this point in the history
Add grammar of TypeScript.
  • Loading branch information
mjambon committed May 14, 2020
1 parent 4a2ef42 commit f623b5f
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .ocp-indent
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See https://github.com/OCamlPro/ocp-indent/blob/master/.ocp-indent for more

# Indent for clauses inside a pattern-match (after the arrow):
# match foo with
# | _ ->
# ^^^^bar
# the default is 2, which aligns the pattern and the expression
match_clause = 4

# When nesting expressions on the same line, their indentation are in
# some cases stacked, so that it remains correct if you close them one
# at a line. This may lead to large indents in complex code though, so
# this parameter can be used to set a maximum value. Note that it only
# affects indentation after function arrows and opening parens at end
# of line.
#
# for example (left: `none`; right: `4`)
# let f = g (h (i (fun x -> # let f = g (h (i (fun x ->
# x) # x)
# ) # )
# ) # )
max_indent = 2
4 changes: 3 additions & 1 deletion scripts/install-tree-sitter
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ project_root=$(git rev-parse --show-toplevel)
# The tree-sitter executable will be in 'node_modules/.bin'.
(
cd "$project_root"
npm install tree-sitter tree-sitter-cli

# tree-sitter-javascript is needed to generate grammar.json for typescript.
npm install tree-sitter tree-sitter-cli tree-sitter-javascript
)
2 changes: 2 additions & 0 deletions src/lib/AST_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ let rec translate (x : Tree_sitter_t.rule_body) =
| PREC_LEFT (_opt_prio, x) -> translate x
| PREC_RIGHT (_opt_prio, x) -> translate x
| FIELD (_name, x) -> translate x (* TODO not sure about ignoring this *)
| ALIAS alias ->
translate alias.content (* TODO probably good to not ignore *)
| IMMEDIATE_TOKEN x -> translate x (* TODO check what this is *)
| TOKEN x -> translate x

Expand Down
1 change: 1 addition & 0 deletions src/lib/Json_rule_adapter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let normalize_variant_object fields =
| "PREC_DYNAMIC" -> Some (`List [get "value"; get "content"])
| "PREC_LEFT" -> Some (`List [opt "value"; get "content"])
| "PREC_RIGHT" -> Some (`List [opt "value"; get "content"])
| "ALIAS" -> Some (`Assoc fields)
| "FIELD" -> Some (`List [get "name"; get "content"])
| "IMMEDIATE_TOKEN" -> Some (get "content")
| "TOKEN" -> Some (get "content")
Expand Down
9 changes: 8 additions & 1 deletion src/lib/Tree_sitter.atd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type grammar = {
~extras: rule_body list; (* tokens (can only be STRING or PATTERN) *)
~inline: ident list; (* rule names *)
~conflicts: ident list list; (* list of lists of rule names *)
~externals: ident list; (* list of token names *)
~externals: rule_body list; (* list of token names? *)
?word: ident option; (* token name *)
~supertypes: ident list; (* rule names *)
}
Expand All @@ -30,8 +30,15 @@ type rule_body = [
| PREC_DYNAMIC of (int * rule_body)
| PREC_LEFT of (int nullable * rule_body)
| PREC_RIGHT of (int nullable * rule_body)
| ALIAS of alias
| FIELD of (ident * rule_body)
| IMMEDIATE_TOKEN of rule_body (* presumably must contain a token *)
| TOKEN of rule_body
] <json adapter.ocaml="Json_rule_adapter"> (* see Json_rule_adapter.ml *)
<ocaml repr="classic">

type alias = {
value: ident;
named: bool;
content: rule_body;
}
5 changes: 5 additions & 0 deletions tests/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The `grammar.js` for typescript was assembled from two files from the
tree-sitter-typescript repo. It's special because another dialect called 'tsx'
is also supported.

Additionally, we have to install tree-sitter-javascript.
Loading

0 comments on commit f623b5f

Please sign in to comment.