-
Notifications
You must be signed in to change notification settings - Fork 861
hrw4u: type the AST and add a stage-inspection tool #13180
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
14
commits into
apache:master
Choose a base branch
from
juanthropic:hrw4u-ast-nodes-spans
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
14 commits
Select commit
Hold shift + click to select a range
a826459
hrw4u: rename ASTVisitor to ASTBuilder
juanthropic f339bc8
hrw4u: introduce thin AST enums for operators and var-section kind
juanthropic 07651bf
hrw4u: add hrw4u-ir tool for inspecting intermediate representations
juanthropic 73a49b8
hrw4u: wire ASTBuilder operators to the new enums
juanthropic 030d101
hrw4u: cover remaining AST builder gaps in test_ast_builder
juanthropic bfc6dc0
hrw4u: rename hrw4u-ir to hrw4u-ast and default to ast stage
juanthropic c914e54
hrw4u: name the '!' token in the grammar to retire textual scans
juanthropic a68b405
hrw4u: tidy ast_nodes.py — explain the enum repr trick and drop Union
juanthropic 0bf3281
hrw4u: note that Target is a value class, not an AST node
juanthropic 315e34b
hrw4u: namespace ast_nodes imports in the builder
juanthropic c362a90
hrw4u: namespace ast_nodes imports in test_ast_builder
juanthropic c959262
hrw4u: name AST value fields after what they actually contain
juanthropic 10acb79
hrw4u: don't package or build a binary for hrw4u-ast
juanthropic e32c1ab
hrw4u: Run format
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
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,82 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # 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. | ||
| """hrw4u-ast - Inspect the HRW4U AST and the stages around it (CST, ...).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import pprint | ||
| import sys | ||
| from typing import Any, Callable | ||
|
|
||
| from antlr4 import CommonTokenStream, InputStream | ||
|
|
||
| from hrw4u.ast_builder import ASTBuilder | ||
| from hrw4u.hrw4uLexer import hrw4uLexer | ||
| from hrw4u.hrw4uParser import hrw4uParser | ||
|
|
||
|
|
||
| def emit_cst(tree: Any, parser: hrw4uParser) -> None: | ||
| print(tree.toStringTree(recog=parser)) | ||
|
|
||
|
|
||
| def emit_ast(tree: Any, _parser: hrw4uParser) -> None: | ||
| ast = ASTBuilder().visit(tree) | ||
| pprint.pp(ast) | ||
|
|
||
|
|
||
| # Stage registry. Adding a new stage (resolved, validated, ...) is a one-line | ||
| # addition here plus its emit_* function above. Each emitter takes the parse | ||
| # tree and the parser (some stages need the parser for token/rule names). | ||
| STAGES: dict[str, Callable[[Any, hrw4uParser], None]] = { | ||
| "cst": emit_cst, | ||
| "ast": emit_ast, | ||
| } | ||
|
|
||
| DEFAULT_STAGE = "ast" | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser( | ||
| description="Inspect the HRW4U AST and surrounding stages.", | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| epilog="Stages:\n cst ANTLR concrete syntax tree (raw parse tree)\n ast dataclass AST built by ASTBuilder (default)\n") | ||
| parser.add_argument( | ||
| "input_file", nargs="?", type=argparse.FileType("r"), default=sys.stdin, help="HRW4U source file (default: stdin)") | ||
| parser.add_argument( | ||
| "--stage", choices=sorted(STAGES.keys()), default=DEFAULT_STAGE, help=f"Which stage to emit (default: {DEFAULT_STAGE})") | ||
| args = parser.parse_args() | ||
|
|
||
| content = args.input_file.read() | ||
| if args.input_file is not sys.stdin: | ||
| args.input_file.close() | ||
|
|
||
| token_stream = CommonTokenStream(hrw4uLexer(InputStream(content))) | ||
| antlr_parser = hrw4uParser(token_stream) | ||
| tree = antlr_parser.program() | ||
|
|
||
| if antlr_parser.getNumberOfSyntaxErrors() > 0: | ||
| print("Parse failed: syntax errors above.", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| STAGES[args.stage](tree, antlr_parser) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
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.