-
Notifications
You must be signed in to change notification settings - Fork 855
hrw4u: Add AST for static analysis and codegen #13126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juanthropic
wants to merge
5
commits into
apache:master
Choose a base branch
from
juanthropic:hrw4u-linter-ast
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b8a0c8c
Add AST node dataclasses for hrw4u linter
juanthropic 2e27ae9
Add AST visitor to build AST from ANTLR parse tree
juanthropic 971e10d
Add AST visitor tests
juanthropic d7f20b6
Add tagged Value type to AST for codegen support
juanthropic af33b4d
Handle commentLine explicitly in visitProgram
juanthropic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import Union | ||
|
|
||
|
|
||
| class ValueKind(Enum): | ||
| STRING = "string" | ||
| IDENT = "ident" | ||
| PARAM_REF = "param_ref" | ||
| IP = "ip" | ||
| REGEX = "regex" | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Value: | ||
| raw: str | ||
| kind: ValueKind | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Node: | ||
| line: int | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Target: | ||
| namespace: str | None | ||
| field: str | ||
|
|
||
| @staticmethod | ||
| def from_dotted(name: str) -> Target: | ||
| # TODO: the grammar lexes dotted paths as a single IDENT token; | ||
| # ideally the grammar would split namespace/field so this | ||
| # heuristic isn't needed. | ||
| dot = name.rfind(".") | ||
| if dot == -1: | ||
| return Target(namespace=None, field=name) | ||
| return Target(namespace=name[:dot], field=name[dot + 1:]) | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Assignment(Node): | ||
| target: Target | ||
| operator: str # "=" or "+=" | ||
| value: Value | int | bool | tuple | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class FunctionCall(Node): | ||
| name: str | ||
| args: tuple[Value | int | bool, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Break(Node): | ||
| pass | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Comparison(Node): | ||
| left: Value | FunctionCall | ||
| operator: str # "==", "!=", ">", "<", "~", "!~", "in", "!in" | ||
| right: Value | int | bool | tuple | ||
| modifiers: tuple[str, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class LogicalOp(Node): | ||
| operator: str # "&&" or "||" | ||
| left: ConditionExpr | ||
| right: ConditionExpr | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class NotOp(Node): | ||
| operand: ConditionExpr | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class BoolLiteral(Node): | ||
| value: bool | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IdentCondition(Node): | ||
| name: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ElifBranch(Node): | ||
| condition: ConditionExpr | ||
| body: tuple[BodyNode, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IfBlock(Node): | ||
| condition: ConditionExpr | ||
| body: tuple[BodyNode, ...] | ||
| elif_branches: tuple[ElifBranch, ...] | ||
| else_body: tuple[BodyNode, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Section(Node): | ||
| type: str | ||
| body: tuple[BodyNode, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ProcParam(Node): | ||
| name: str | ||
| default: Value | int | bool | None | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class VarDecl(Node): | ||
| name: str | ||
| type_name: str | ||
| slot: int | None | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class VarSection(Node): | ||
| scope: str | ||
| declarations: tuple[VarDecl, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class UseDirective(Node): | ||
| spec: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ProcedureDecl(Node): | ||
| name: str | ||
| params: tuple[ProcParam, ...] | ||
| body: tuple[BodyNode, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class HRW4UAST: | ||
| body: tuple[TopLevelNode, ...] | ||
|
|
||
|
|
||
| # Type aliases: must follow all class definitions (evaluated at runtime). | ||
| ConditionExpr = Union[Comparison, LogicalOp, NotOp, BoolLiteral, IdentCondition, FunctionCall] | ||
| BodyNode = Union[Assignment, FunctionCall, IfBlock, Break] | ||
| TopLevelNode = Union[UseDirective, VarSection, ProcedureDecl, Section] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in d7f20b6. Added
Valuedataclass withValueKindenum to preserve semantic distinction between string literals, identifiers, param refs, IPs, and regexes.