tree-sitter-regex
Regex grammar for tree-sitter
Tree-sitter parser for regular expressions
License: MIT License
Regex grammar for tree-sitter
According to https://github.com/tree-sitter/tree-sitter-regex/blob/master/corpus/main.txt ,
current terminology of Tree sitter is aligned with PCRE pattern for
But not with
i suggest to keep pcre naming and use alternative instead of disjunction.
Abbreviations come easy too.
Consider (rep (alt 0 1) 1 n)
for binary integer.
Very simple to read, simple to write and to understand.
Those ones could also help to get a leaner ast
$ echo "/(?<!not) preceeding/" > re.re
$ tree-sitter parse re.re
(pattern [0, 0] - [1, 0]
(ERROR [0, 0] - [0, 9]
(pattern [0, 0] - [0, 8]
(term [0, 0] - [0, 8]
(pattern_character [0, 0] - [0, 1])
(ERROR [0, 1] - [0, 5])
(pattern_character [0, 5] - [0, 6])
(pattern_character [0, 6] - [0, 7])
(pattern_character [0, 7] - [0, 8]))))
(term [0, 9] - [0, 21]
(pattern_character [0, 9] - [0, 10])
(pattern_character [0, 10] - [0, 11])
(pattern_character [0, 11] - [0, 12])
(pattern_character [0, 12] - [0, 13])
(pattern_character [0, 13] - [0, 14])
(pattern_character [0, 14] - [0, 15])
(pattern_character [0, 15] - [0, 16])
(pattern_character [0, 16] - [0, 17])
(pattern_character [0, 17] - [0, 18])
(pattern_character [0, 18] - [0, 19])
(pattern_character [0, 19] - [0, 20])
(pattern_character [0, 20] - [0, 21])))
re.re 0 ms (ERROR [0, 0] - [0, 9])
$ cat shorthand.re
/[\w-.]/
$ tree-sitter parse shorthand.re
(pattern [0, 0] - [1, 0]
(term [0, 0] - [0, 8]
(pattern_character [0, 0] - [0, 1])
(character_class [0, 1] - [0, 7]
(class_range [0, 2] - [0, 6]
(character_class_escape [0, 2] - [0, 4])
(class_character [0, 5] - [0, 6])))
(pattern_character [0, 7] - [0, 8])))
expected: error
Hello !
I noticed that there is an ERROR type node when I try to parse regex expression like this "[a-]" but not with "[-a]" or "[-]" am I doing something wrong ?
>>> parser.parse("[a-]").root_node.sexp()
(pattern (term (character_class (class_character) (ERROR))))
>>> parser.parse("[-a]").root_node.sexp()
(pattern (term (character_class (class_character) (class_character))))
I believe that in this case tree sitter makes a confusion with class range nodes. Is there any workaround to this issue ?
ps: I am using the tree-sitter 0.19 version
I tried to fix this problem:
$ echo '(a)\1' > /tmp/decimal-backreference.txt
$ node_modules/.bin/tree-sitter parse /tmp/decimal-backreference.txt
(pattern [0, 0] - [1, 0]
(term [0, 0] - [0, 3]
(anonymous_capturing_group [0, 0] - [0, 3]
(pattern [0, 1] - [0, 2]
(term [0, 1] - [0, 2]
(pattern_character [0, 1] - [0, 2])))))
(ERROR [0, 3] - [0, 5]
(ERROR [0, 3] - [0, 5])))
/tmp/decimal-backreference.txt 0 ms (ERROR [0, 3] - [0, 5])
... in these two branches of mine:
Why is that? (As a general question: What's the best place to get help with problems writing tree-sitter grammars?)
=> echo '{a' > test.re
=> tree-sitter parse test.re
(pattern [0, 0] - [1, 0]
(ERROR [0, 0] - [0, 1])
(term [0, 1] - [0, 2]
(pattern_character [0, 1] - [0, 2])))
test.re 0 ms ERROR [0, 0] - [0, 1]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.