Class Parser
Parses a document based on a Language to produce a Tree.
Analogous to TSParser
.
Functions
Parser.new () | Create a new parser. |
Methods
Parser:language () | Get the parser's current language. |
Parser:set_language (lang) | Set the language that the parser should use for parsing. |
Parser:included_ranges () | Get the parser's included ranges. |
Parser:set_included_ranges (ranges) | Set the parser's included ranges. |
Parser:parse (old_tree, read) | Use the parser to parse some source code and create a syntax tree. |
Parser:parse_ (old_tree, read) | Like Parser.parse, but the reader function returns a second integer
value representing the offset at which the requested chunk of text starts
within the string. |
Functions
Methods
- Parser:language ()
-
Get the parser's current language.
Returns:
- Parser:set_language (lang)
-
Set the language that the parser should use for parsing.
Parameters:
- lang Language
- Parser:included_ranges ()
-
Get the parser's included ranges.
Returns:
- Parser:set_included_ranges (ranges)
-
Set the parser's included ranges.
Passing
nil
or an empty table makes the parser include the entire document.The given ranges must be sorted in ascending order, and be non-overlapping.
Parameters:
- ranges Range.Array
Raises:
Ranges are not in ascending order and non-overlapping. - Parser:parse (old_tree, read)
-
Use the parser to parse some source code and create a syntax tree.
If you are parsing this document for the first time, pass
nil
for theold_tree
parameter.Otherwise, pass the previous syntax tree for the
old_tree
parameter. For this to work correctly, you must have already edited the old syntax tree using Tree:edit so that it matches the source code changes.The reader function takes a byte offset and a Point, and returns a string containing the text inside the document at the position specified.
An optional
integer
offset can be returned, such thatreturn text:sub(n)
is equivalent toreturn text, n
. The latter is faster. The implementation handles negative indices the same way as string:sub.Return an empty string or
nil
to signal end of the document. Note that ifn > #text
, it is counted as an empty string.Parameters:
- old_tree Tree edited syntax tree
- read function(integer,Point):string,integer reader function
Returns:
- Parser:parse_ (old_tree, read)
-
Like Parser.parse, but the reader function returns a second
integer
value representing the offset at which the requested chunk of text starts within the string.This is provided so you can avoid having to create substrings, improving performance.
Parameters:
- old_tree Tree edited syntax tree
- read function(integer,Point):string,integer reader function
Returns: