Skip to content
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

Added Python3 target support for the Rust Grammar. #4204

Merged
merged 4 commits into from
Aug 19, 2024

Conversation

Webstonesl
Copy link
Contributor

Updated the RustParser to use the rulenames of the Lexer and added Python3 support. A separate Lexer / Parser was needed due to the target language specific semantic and lexical predicates.

@kaby76
Copy link
Contributor

kaby76 commented Aug 13, 2024

Thanks for the contribution.

  • There is no such thing as the "Python" target. It's only "Python3" because the only way to generate the parser for Python3 is "antlr4 -Dlanguage=Python3 *.g4". There used to be a "Python2" target, but it was removed because it was a burden to keep it updated. The Python3 target cannot be compiled in Python2. Rename Python/ to Python3/.
  • Grammar forking (making a separate .g4 for parser and lexer) is supported by trgen, but it is discouraged (I will change the code). I recommend that you write this in "target agnostic format". You are almost there anyway because the difference is between "this" and "self". The established way to fix this "final", incompatibility in syntax between targets is to use "transformGrammar.py". This file is used to modify the .g4's prior to running the Antlr4 Tool. Copy this file to Python3/. The Python3 script is more or less the same for all the grammars.
  • The grammar for Rust is really slow. While there is no ambiguity in the grammar, it contains numerous full-context fallbacks. To verify, generate the CSharp target, and use trperf.
https://github.com/Webstonesl/antlr-grammars-v4/blob/200df7ebd307cfddd67920c71866f996b3fa0a9f/rust/desc.xml. This is required to enable testing of the target. But, because the parser performs so poorly, we'll have to specify a very small subset of tests for Python3.
$ trperf -c 'Frf' ../examples/*.rs | sort -k3 -n -r | head -22
Time to parse: 00:00:00.1034523
Time to parse: 00:00:06.1796782
Time to parse: 00:00:00.0012821
Time to parse: 00:00:00.0122744
Time to parse: 00:00:08.0654020
Time to parse: 00:00:00.0096744
Time to parse: 00:00:01.4000653
Time to parse: 00:00:00.0362163
Time to parse: 00:00:00.9897729
Time to parse: 00:00:01.0934857
Time to parse: 00:00:00.5731150
Time to parse: 00:00:00.0020088
Time to parse: 00:00:00.0005721
Time to parse: 00:00:00.0459645
Time to parse: 00:00:00.0080398
Time to parse: 00:00:00.0057590
../examples/deno_core_runtime.rs        expression      1180
../examples/deno_core_runtime.rs        expression      1047
../examples/intellijrust_test_allinone.rs       tokenTree       1019
../examples/deno_core_runtime.rs        expression      533
../examples/deno_core_runtime.rs        tokenTree       468
../examples/deno_core_runtime.rs        patternWithoutRange     460
../examples/rustls_quic.rs      tokenTree       390
../examples/deno_core_runtime.rs        typeNoBounds    371
../examples/deno_core_runtime.rs        type_   339
../examples/ssrust_ssserver.rs  tokenTree       245
../examples/ssrust_config.rs    expression      234
../examples/intellijrust_test_allinone.rs       type_   220
../examples/intellijrust_test_allinone.rs       typeNoBounds    219
../examples/rustls_quic.rs      expression      210
../examples/rustls_quic.rs      expression      209
../examples/ssrust_config.rs    expression      169
../examples/leaf_vmessstream.rs expression      164
../examples/ssrust_config.rs    typeNoBounds    163
../examples/leaf_vmessstream.rs expression      154
../examples/deno_core_runtime.rs        genericArgs     154
../examples/ssrust_config.rs    type_   144
../examples/rustls_quic.rs      typeNoBounds    129
08/13-07:01:50 ~/issues/g4-4204/rust/Generated-CSharp
$

So, to avoid time-outs for the Python3 target, partition the tests by size. For example:

08/13-07:46:29 ~/issues/g4-4204/rust/examples
$ ls -R
.:
long/  quick/

./long:
deno_core_runtime.rs            intellijrust_test_allinone.rs.tree  literal.rs           ssrust_config.rs.tree
deno_core_runtime.rs.tree       issue_1985_raw_string.rs            literal.rs.tree      ssrust_ssserver.rs
inlinepython_example.rs         issue_1985_raw_string.rs.tree       rustls_quic.rs       ssrust_ssserver.rs.tree
inlinepython_example.rs.tree    leaf_vmessstream.rs                 rustls_quic.rs.tree
intellijrust_test_allinone.rs*  leaf_vmessstream.rs.tree            ssrust_config.rs

./quick:
comment.rs       v1_46_0_split_float_literal.rs       v1_53_0_or_pattern.rs               v1_56_0_open_range.rs
comment.rs.tree  v1_46_0_split_float_literal.rs.tree  v1_53_0_or_pattern.rs.tree          v1_56_0_open_range.rs.tree
hello.rs         v1_48_0_unsafe_mod.rs                v1_53_0_unicode_identifier.rs
hello.rs.tree    v1_48_0_unsafe_mod.rs.tree           v1_53_0_unicode_identifier.rs.tree
08/13-07:46:33 ~/issues/g4-4204/rust/examples
$

Then, update the desc.xml to this:

<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
   <antlr-version>^4.7</antlr-version>
   <targets>Cpp;CSharp;Java;Python3</targets>
   <test>
      <targets>Cpp;CSharp;Java</targets>
      <inputs>examples/**/*.rs</inputs>
   </test>
   <test>
      <targets>Python3</targets>
      <inputs>examples/quick/*.rs</inputs>
   </test>
</desc>

@teverett teverett added the rust label Aug 16, 2024
@teverett
Copy link
Member

@kaby76 this is not quite ready to pull yet?

@kaby76
Copy link
Contributor

kaby76 commented Aug 17, 2024

@kaby76 this is not quite ready to pull yet?

@teverett Looks good to go.

@kaby76
Copy link
Contributor

kaby76 commented Aug 19, 2024

@teverett Actually, we need to run the workflow. Thanks.

@teverett
Copy link
Member

@Webstonesl thanks!

@teverett teverett merged commit b4cd348 into antlr:master Aug 19, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants