-
Notifications
You must be signed in to change notification settings - Fork 857
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
13
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
13 commits
Select commit
Hold shift + click to select a range
a9ada8b
Add AST node dataclasses for hrw4u linter
juanthropic 8cf91ac
Add AST visitor to build AST from ANTLR parse tree
juanthropic d369001
Add AST visitor tests
juanthropic 3898823
Add tagged Value type to AST for codegen support
juanthropic 8b9cf35
Handle commentLine explicitly in visitProgram
juanthropic 2685f26
Replace tagged Value type with concrete value types
juanthropic 8084e00
Raise on unhandled grammar alternatives in AST visitor
juanthropic 6a4d45b
Format code
juanthropic 8ad960a
Add tests for comment skipping and modifier casing
juanthropic 59b1e5d
Add __all__ to ast_nodes.py for safe wildcard imports
juanthropic 8f5a1bb
Use wildcard imports from ast_nodes
juanthropic 639f477
Add return type annotations to ASTVisitor methods
juanthropic 04837ad
Collapse short constructor calls to single lines
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,211 @@ | ||
| # | ||
| # 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 typing import Union | ||
|
|
||
| __all__ = [ | ||
| "LiteralStringValue", | ||
| "IdentValue", | ||
| "IPValue", | ||
| "ParamRef", | ||
| "RegexValue", | ||
| "ValueExpr", | ||
| "Node", | ||
| "Target", | ||
| "Assignment", | ||
| "FunctionCall", | ||
| "Break", | ||
| "Comparison", | ||
| "LogicalOp", | ||
| "NotOp", | ||
| "BoolLiteral", | ||
| "IdentCondition", | ||
| "ElifBranch", | ||
| "IfBlock", | ||
| "Section", | ||
| "ProcParam", | ||
| "VarDecl", | ||
| "VarSection", | ||
| "UseDirective", | ||
| "ProcedureDecl", | ||
| "HRW4UAST", | ||
| "ConditionExpr", | ||
| "BodyNode", | ||
| "TopLevelNode", | ||
| ] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class LiteralStringValue: | ||
| raw: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IdentValue: | ||
| raw: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IPValue: | ||
| raw: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ParamRef: | ||
| raw: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class RegexValue: | ||
| raw: str | ||
|
|
||
|
|
||
| ValueExpr = Union[LiteralStringValue, IdentValue, IPValue, ParamRef, int, bool, tuple[IPValue, ...]] | ||
|
|
||
|
|
||
| @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: ValueExpr | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class FunctionCall(Node): | ||
| name: str | ||
| args: tuple[ValueExpr, ...] | ||
|
|
||
|
juanthropic marked this conversation as resolved.
zwoop marked this conversation as resolved.
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Break(Node): | ||
| pass | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class Comparison(Node): | ||
| left: IdentValue | FunctionCall | ||
| operator: str # "==", "!=", ">", "<", "~", "!~", "in", "!in" | ||
| right: ValueExpr | RegexValue | tuple[ValueExpr, ...] | ||
| 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: ValueExpr | None | ||
|
|
||
|
zwoop marked this conversation as resolved.
|
||
|
|
||
| @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.