Skip to content

Commit c8982dd

Browse files
committedJul 28, 2022
Add partly written documentation for antlr parser
1 parent 0af7dcc commit c8982dd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 

‎docs/GrammarParser.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Grammar Parser
2+
3+
This documentation details the design and implementation of the Harmony programming language parser.
4+
5+
- [Grammar Parser](#grammar-parser)
6+
- [Modifying Harmony.g4](#modifying-harmonyg4)
7+
- [Expressions](#expressions)
8+
- [Statements](#statements)
9+
- [Embedded Python Code in `Harmony.g4`](#embedded-python-code-in-harmonyg4)
10+
- [Modifying `custom_denter.py`](#modifying-custom_denterpy)
11+
12+
## Modifying Harmony.g4
13+
14+
The `Harmony.g4` defines the grammar rules (statements and expressions) for the Harmony programming
15+
language written using `ANTLR 4.9.3`.
16+
17+
> **NOTE**: The version of the Python3 antlr4 runtime must match
18+
the parser generator version exactly. That is, if the ANTLR parser generator version is change
19+
to `ANTLR x.y.z`, then the `antlr4-python3-runtime==x.y.z` pip package has to be set as the
20+
compiler's dependency.
21+
22+
If the `Harmony.g4` is modified, then run `make parser` to generate the updated `HarmonyLexer.py`,
23+
`HarmonyParser.py`, and `HarmonyVisitor.py` files. **Any changes to `HarmonyVisitor.py`, such as new/deleted/modified method headers must be reflected in the HarmonyVisitor implementation `antlr_rule_visitor.py`**.
24+
25+
### Expressions
26+
27+
### Statements
28+
29+
### Embedded Python Code in `Harmony.g4`
30+
31+
## Modifying `custom_denter.py`
32+
33+
The `ModifiedDenterHelper` class implemented in this file extends the `DenterHelper` class of the
34+
`antlr-denter` dependency and overrides the internal behavior. The main behavior of interest is handling
35+
expressions that are broken into separate lines. Without modification, the default `DenterHelper` class would create `INDENT` and `DEDENT` tokens in between expression tokens, which would be difficult to parse using ANTLR 4. The extended implementation adds a check when the current token is a new indent while the previous observed token is an operator, such as `+` or `/`, which would be followed by another expression. If that's the case, then the observed `INDENT` (and the `DEDENT` token that would have also been output) is ignored.
36+

0 commit comments

Comments
 (0)
Please sign in to comment.