Skip to content

Commit d63e5c0

Browse files
committed
Prepare for public commit history
We want commit messages to represent finshed pieces of work. So try to start with a clean slate.
1 parent a7a8ab7 commit d63e5c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1587
-930
lines changed

Diff for: .gitignore

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
target/
22
/Cargo.lock
33
dev_docs/diagrams
44
crates/oq3_syntax/examples/oq3_source
@@ -7,6 +7,11 @@ organization.md
77
parser_design.md
88
restorenodes.sh
99
restorgenerated.sh
10-
crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
10+
crates/parser/src/syntax_kind/_syntax_kind_enum.rs
1111
crates/oq3_syntax/src/ast/generated/_new_nodes.rs
1212
crates/oq3_syntax/src/ast/generated/_new_tokens.rs
13+
.venv/
14+
.venvs/
15+
.env/
16+
.envs/
17+
.ackrc

Diff for: .rustfmt.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=
2+
3+
# Use this when it becomes stable
4+
# reorder_impl_items = true
5+
6+
# When use_small_heuristics is set to Max, then each granular width setting is set to the same value as max_width.
7+
# use_small_heuristics = "Max"

Diff for: CODE_OF_CONDUCT.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Code of Conduct
2+
All members of this project agree to adhere to the Qiskit Code of Conduct as found
3+
at [https://github.com/Qiskit/qiskit/blob/master/CODE_OF_CONDUCT.md](https://github.com/Qiskit/qiskit/blob/master/CODE_OF_CONDUCT.md)
4+
5+
----
6+
7+
License: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/),
8+
Copyright Contributors to Qiskit.

Diff for: CONTRIBUTING.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Pull Requests
2+
3+
Running `cargo clippy` on the branch to be merged must show no errors.
4+
To handle a lot of errors at the command line can use (for unix-like OS) `cargo clippy --color always &| less -R`.
5+
6+

Diff for: Cargo.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ authors = ["OpenQASM3 parser team"]
2727
# # Set this to 1 or 2 to get more useful backtraces in debugger.
2828
# debug = 0
2929

30-
[patch.'crates-io']
31-
# rowan = { path = "../rowan" }
32-
3330
[workspace.dependencies]
3431
# local crates
3532

36-
oq3_lexer = { path = "./crates/oq3_lexer", version = "0.0.0" }
37-
oq3_parser = { path = "./crates/oq3_parser", version = "0.1.0" }
33+
lexer = { path = "./crates/lexer", version = "0.0.0" }
34+
parser = { path = "./crates/parser", version = "0.1.0" }
3835
oq3_syntax = { path = "./crates/oq3_syntax", version = "0.0.0" }
39-
semantic_ast = { path = "./crates/semantic_ast", version = "0.0.0" }
36+
semantics = { path = "./crates/semantics", version = "0.0.0" }
4037
sourcegen = { path = "./crates/sourcegen", version = "0.0.0" }
4138
ast_pyo3 = { path = "./crates/ast_pyo3", version = "0.0.0" }
4239
source_file = { path = "./crates/source_file", version = "0.0.0" }

Diff for: NOTICE.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
The code in crates `lexer`, `parser`, and `oq3_syntax` are derived
2+
from crates in the rust-analyzer project, commit (d398a).
3+
The code in these crates is licensed in this OpenQASM 3 parser project under
4+
the following MIT license:
5+
6+
Permission is hereby granted, free of charge, to any
7+
person obtaining a copy of this software and associated
8+
documentation files (the "Software"), to deal in the
9+
Software without restriction, including without
10+
limitation the rights to use, copy, modify, merge,
11+
publish, distribute, sublicense, and/or sell copies of
12+
the Software, and to permit persons to whom the Software
13+
is furnished to do so, subject to the following
14+
conditions:
15+
16+
The above copyright notice and this permission notice
17+
shall be included in all copies or substantial portions
18+
of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
21+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
22+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
24+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28+
DEALINGS IN THE SOFTWARE.

Diff for: README.md

+51-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
# OpenQASM 3 Parser
22

3-
This is a parser for the OpenQASM 3 language (OQ3) written in Rust.
3+
This project provides a compiler front end for OpenQASM 3 language (OQ3).
4+
45
In this document, this parser is referred to as `openqasm3_parser`.
56

7+
Differences with the [OpenQASM reference parser](https://github.com/openqasm/openqasm) are
8+
9+
* The parser in `openqasm3_parser` is much more performant.
10+
A crude test with large source files showed parse time reduced by a factor of 80.
11+
* `openqasm3_parser` performs semantic analysis.
12+
13+
<details>
14+
<summary>What is a rust "crate"</summary>
15+
616
We talk about rust ["crates"](https://doc.rust-lang.org/book/ch07-01-packages-and-crates.html).
717
A rust library crate is more or less the source for a rust library that is developed, built, and installed with the rust package manager [cargo](https://doc.rust-lang.org/cargo/).
818
This single repository contains more than one separately installable crates. In the future, this repository may also be used to generate other artifacts.
919

10-
The code does not yet perfectly do everything promised below.
20+
</details>
21+
22+
### Status
23+
24+
There have been no releases of any kind. I'll try to keep something more or less meaningful in this spot.
25+
For instance, I hope to soon replace "I" with "we".
1126

1227
### Crates (roughly one installable library per crate)
1328

14-
* [oq3_lexer](./crates/oq3_lexer) -- This is a lightly modified version of the `rustc` (the rust compiler) lexer.
15-
* [oq3_parser](./crates/oq3_parser) -- This parses OQ3 source as lexed by `oq3_lexer` into a nested sequence of tagged sections of text
16-
according the OQ3 grammar. This is based on the rust-analyzer parser crate. All nodes are represented by an instance
17-
of a single Rust `struct` (with some support `structs`).
18-
* [oq3_syntax](./crates/oq3_syntax) -- This crate transforms the AST produced by `oq3_parser` into an AST implemented
19-
as standard rust data structures: `enum` and `struct` and provides an interface for working with the AST.
20-
The types of these data structures reflect the syntactic elements of OQ3. The rust-analyzer
21-
documentation sometimes refers to this AST by something like "typed AST". This can be confusing. It does not mean that semantic
22-
analysis has been performed and OQ3 types have been assigned to all expressions. Rather it is meant to highlight the distinction:
23-
The AST produced by `oq3_syntax` represents syntactic elements using the rust type system. The lower AST produced by
24-
`oq3_parser` represents syntactic elements via tags.
25-
* [semantic_ast](./crates/semantic_ast) -- Generates an AST for OQ3 with the types of all typed OQ3 elements resolved and
26-
the referent of all identifiers resolved.
27-
* [source_file](./crates/source_file) -- A higher-level interface to the syntactic AST. This sits beetween the syntactic and
28-
semantic ASTs. This crate manages source files, and incuded source files, and diagnostic messages.
29-
* [ast_pyo3](./crates/source_file) A Python interface. This is a bit exploratory. But fixing the approach is a development priority.
29+
The first three crates are based on tools for `rust` and `rust-analyzer`.
30+
31+
* [lexer](./crates/lexer) -- A lightly modified version of the `rustc` (the rust compiler) lexer.
32+
* [parser](./crates/parser) -- Ingests output of `lexer` and outputs a concrete syntax tree.
33+
* [oq3_syntax](./crates/oq3_syntax) -- Ingests output of `parser` and outputs an abstract syntax tree (AST).
34+
The rust-analyzer [documentation](#design) sometimes refers to this AST by something like "typed AST".
35+
This can be confusing. It does not mean that semantic
36+
analysis has been performed and OQ3 types have been assigned to all expressions. It means that the rust type system is
37+
used to encode syntactic elements, in contrast to some lower representations in the same crate.
38+
* [semantics](./crates/semantics) -- Performs [semantic analysis](https://en.wikipedia.org/wiki/Compiler#Front_end)
39+
and outputs an [abstract semantic graph (ASG)](https://en.wikipedia.org/wiki/Abstract_semantic_graph)
40+
There are other names for this structure. But "ASG" is convenient.
41+
* [source_file](./crates/source_file) -- A higher-level interface to the syntactic AST. This sits beetween the syntactic AST and
42+
semantic ASG. This crate manages the main source file and incuded source files.
43+
* [ast_pyo3](./crates/source_file) Experimental code. It will not be used in it's current form.
3044

3145
#### Supporting crates
3246

@@ -38,10 +52,27 @@ crate has a bug.
3852
Do not run `cargo test`. Rather use `./run_tests.sh` or commands found therein. This is because codegen is implemented via
3953
the test system (you read correctly). If possible, we plan to change this to a more conventional approach.
4054

55+
### Using this front end
56+
57+
A reminder: A front end is not of much use unless you have a back end. Examples showing the entry points and how to use them,
58+
can be found in [./crates/semantics/examples/semdemo.rs](./crates/semantics/examples/semdemo.rs).
59+
60+
```shell
61+
shell> export QASM3_PATH=./crates/semantics/examples/qasm/
62+
shell> cargo run --example semdemo -- semantic scratch1.qasm
63+
```
64+
65+
Replace `scratch1.qasm` with some file found in [./crates/semantics/examples/qasm/](./crates/semantics/examples/qasm/).
66+
67+
#### Search path
68+
69+
The environment variable `QASM_PATH` is a colon separated list of paths. Note that the name follows the venerable unix tradition of
70+
ending in `PATH` rather than `PATHS`. The code that retrives the paths uses routines `std::path` which may actually handle
71+
path specifications on other platforms.
72+
4173
### Design
4274

4375
Code from rust-analyzer has been borrowed and modified for the lower levels of parsing.
44-
4576
The [developer documents for rust-analyzer](https://github.com/rust-lang/rust-analyzer/tree/master/docs/dev) are very relevant as the
4677
structure has not been changed in adapting to OQ3.
4778

@@ -52,10 +83,9 @@ structure has not been changed in adapting to OQ3.
5283
* [From Pratt to Dijkstra](https://matklad.github.io/2020/04/15/from-pratt-to-dijkstra.html)
5384
* [Resilient parsing](https://matklad.github.io/2023/05/21/resilient-ll-parsing-tutorial.html)
5485

55-
5686
## Notes
5787

58-
Some of this code is modified from code in [rust-analyzer](https://github.com/rust-lang/rust-analyzer).
88+
Some of this code is modified from code found in [rust-analyzer](https://github.com/rust-lang/rust-analyzer).
5989
It was taken at [this commit](https://github.com/rust-lang/rust-analyzer/pull/15380):
6090

6191
commit d398ad3326780598bbf1480014f4c59fbf6461a7

Diff for: codegen_scripts/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Code generation for the crate oq3_syntax
2+
3+
NOTE: These (rather simple) scripts controlling codegen have been developed under fedora linux. They may need
4+
to be modified for your OS. The codegen itself is done in rust and should run correctly on most
5+
platforms, or should be easily made to do so.
6+
7+
The source files in [../crates/oq3_syntax/src/ast/generated](../crates/oq3_syntax/src/ast/generated) are generated via scripts
8+
run during development. They do not need to be run when building the crates if the input
9+
files to the codegen have not been modified.
10+
11+
On the other hand, if the input files to codegen have been modified, then codegen scripts
12+
must be run by hand. Some details are in [./mkgenerated](./mkgenerated).
13+
14+
Run ./mkgenerated to write generated rust source to temporary files.
15+
Run [./cpnodes.sh](./cpnodes.sh) and [./cpgenerated.sh](./cpgenerated.sh) to overwrite the revision-controlled source
16+
files with the temporary files.

Diff for: cleangenerated.sh renamed to codegen_scripts/cleangenerated.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# The generated files that are actually in use, nodes.rs and generated.rs
66
# are not touched by this script.
77

8-
rm crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
9-
rm crates/oq3_parser/src/syntax_kind/syntax_kind_enum.rs.~*
8+
cd ..
9+
rm crates/parser/src/syntax_kind/_syntax_kind_enum.rs
10+
rm crates/parser/src/syntax_kind/syntax_kind_enum.rs.~*
1011

1112
rm crates/oq3_syntax/src/ast/generated/_new_nodes.rs
1213
rm crates/oq3_syntax/src/ast/generated/nodes.rs.~*

Diff for: cpgenerated.sh renamed to codegen_scripts/cpgenerated.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# Copy the generated code from the temporary files to which it is written
55
# to it's final location where it will be compiled into the library.
66

7-
cp -a --backup=t crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs crates/oq3_parser/src/syntax_kind/syntax_kind_enum.rs
7+
cd .. && cp -a --backup=t crates/parser/src/syntax_kind/_syntax_kind_enum.rs crates/parser/src/syntax_kind/syntax_kind_enum.rs
88

Diff for: cpnodes.sh renamed to codegen_scripts/cpnodes.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# Copy the generated code from the temporary files to which it is written
55
# to its final location where it will be compiled into the library.
66

7-
cp -a --backup=t crates/oq3_syntax/src/ast/generated/_new_nodes.rs crates/oq3_syntax/src/ast/generated/nodes.rs
7+
cd .. && cp -a --backup=t crates/oq3_syntax/src/ast/generated/_new_nodes.rs crates/oq3_syntax/src/ast/generated/nodes.rs

Diff for: mk_nodes.sh renamed to codegen_scripts/mk_nodes.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo
66
echo =============== Running sourcegen_ast_nodes once ===============
77
echo
88

9-
cargo test sourcegen_ast_nodes
9+
cd .. && cargo test sourcegen_ast_nodes
1010

1111
# echo
1212
# echo =============== Running sourcegen_ast_nodes twice ===============
@@ -15,5 +15,5 @@ cargo test sourcegen_ast_nodes
1515
# cargo test sourcegen_ast_nodes
1616

1717
# rustfmt crates/oq3_syntax/src/ast/generated/_new_nodes.rs
18-
# rustfmt crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
18+
# rustfmt crates/parser/src/syntax_kind/_syntax_kind_enum.rs
1919
# rustfmt crates/oq3_syntax/src/ast/generated/_new_tokens.rs

Diff for: mk_syntax_kinds.sh renamed to codegen_scripts/mk_syntax_kinds.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo
66
echo ======================= Running write_syntax_kinds_enum once ===============
77
echo
88

9-
cargo test write_syntax_kinds_enum
9+
cd .. && cargo test write_syntax_kinds_enum
1010

1111
#echo ======================= Running write_syntax_kinds_enum() twice ===============
1212
# echo >>>>>>>>>>>>>>> Running write_syntax_kinds_enum() twice
@@ -15,6 +15,6 @@ cargo test write_syntax_kinds_enum
1515
# cargo test write_syntax_kinds_enum
1616

1717
# rustfmt crates/oq3_syntax/src/ast/generated/_new_nodes.rs
18-
# rustfmt crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
18+
# rustfmt crates/parser/src/syntax_kind/_syntax_kind_enum.rs
1919
# rustfmt crates/oq3_syntax/src/ast/generated/_new_tokens.rs
2020

Diff for: mk_syntax_tokens.sh renamed to codegen_scripts/mk_syntax_tokens.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ echo
66
echo =============== Running sourcegen_ast_tokens once ===============
77
echo
88

9-
cargo test sourcegen_ast_tokens
9+
cd .. && cargo test sourcegen_ast_tokens
1010

1111
# rustfmt crates/oq3_syntax/src/ast/generated/_new_nodes.rs
12-
# rustfmt crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
12+
# rustfmt crates/parser/src/syntax_kind/_syntax_kind_enum.rs
1313
# rustfmt crates/oq3_syntax/src/ast/generated/_new_tokens.rs
1414

Diff for: mkgenerated.sh renamed to codegen_scripts/mkgenerated.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
# This should be enough for codegen.
1111

1212
# But you still need to add the things to the parser grammar
13-
# 4 You might need to update crates/oq3_parser/src/grammar/expressions/atom.rs
14-
# Or crates/oq3_parser/src/grammar/items.rs (eg. for `gate`)
13+
# 4 You might need to update crates/parser/src/grammar/expressions/atom.rs
14+
# Or crates/parser/src/grammar/items.rs (eg. for `gate`)
1515
# Or other grammar files
1616

1717
# Generated files are not given their final names in order not to clobber exisiting generated code
1818
# Here are the temporary filenames and the final filenames
1919
# You have to copy them manually.
2020
# crates/oq3_syntax/src/ast/generated/_new_tokens.rs --> tokens.rs
2121
# crates/oq3_syntax/src/ast/generated/_new_nodes.rs --> nodes.rs
22-
# crates/oq3_parser/src/syntax_kind/_generated.rs --> generated.rs
22+
# crates/parser/src/syntax_kind/_generated.rs --> generated.rs
2323

2424
# Update: Running this script now seems robust. Originally all codegen was done
2525
# by a single test. I split it into two tests and run each of them twice.
@@ -38,6 +38,7 @@
3838
# Warning: Do not format till all files are generated.
3939
# Don't know why, but code gen will fail otherwise.
4040

41-
rustfmt crates/oq3_parser/src/syntax_kind/_syntax_kind_enum.rs
41+
cd ..
42+
rustfmt crates/parser/src/syntax_kind/_syntax_kind_enum.rs
4243
rustfmt crates/oq3_syntax/src/ast/generated/_new_nodes.rs
4344
rustfmt crates/oq3_syntax/src/ast/generated/_new_tokens.rs

Diff for: crates/ast_pyo3/buildlib.sh

-9
This file was deleted.

Diff for: crates/ast_pyo3/src/bytecode.rs

-9
This file was deleted.

Diff for: crates/oq3_lexer/Cargo.toml renamed to crates/lexer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "oq3_lexer"
2+
name = "lexer"
33
version = "0.0.0"
44
edition = "2021"
55

File renamed without changes.

Diff for: crates/oq3_lexer/src/lib.rs renamed to crates/lexer/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl Cursor<'_> {
422422
}
423423
}
424424

425-
fn hardware_ident(&mut self) -> TokenKind { // OQ3
425+
fn hardware_ident(&mut self) -> TokenKind {
426426
// debug_assert!(is_id_start(self.prev()));
427427
// Start is already eaten, eat the rest of identifier.
428428
match self.first() {
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: crates/oq3_syntax/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ triomphe.workspace = true
2727
# local crates
2828

2929
# rustc_lexer.workspace = true
30-
oq3_lexer.workspace = true
31-
oq3_parser.workspace = true
30+
lexer.workspace = true
31+
parser.workspace = true
3232
# profile.workspace = true
3333
stdx.workspace = true
3434
# text-edit.workspace = true

Diff for: crates/oq3_syntax/examples/demotest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::path::PathBuf;
33
use clap::{Parser, Subcommand};
44

55
use rowan::NodeOrToken; // TODO: this can be accessed from a higher level
6-
use oq3_lexer::{tokenize, Token};
7-
use oq3_parser::SyntaxKind;
6+
use lexer::{tokenize, Token};
7+
use parser::SyntaxKind;
88
use oq3_syntax::{ast, parse_text, SourceFile, GreenNode};
99

1010
#[derive(Parser)]

Diff for: crates/oq3_syntax/examples/itemparse.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int[64] x = 142;
4040
parse_print_items(code);
4141
}
4242

43-
//use oq3_parser::syntax_kind::SyntaxKind;
43+
//use parser::syntax_kind::SyntaxKind;
4444
fn main () {
4545
// parts_testing();
4646
try_int_def();

0 commit comments

Comments
 (0)