Closed mw66 closed 2 years ago
Not sure if I have time to debug this, but a few quick remarks first:
From https://docs.python.org/3/reference/grammar.html:
We use the | separator to mean PEG’s “ordered choice” (written as / in traditional PEG grammars).
Pegged has the |
operator, but it is a non-PEG extension meaning "longest match", not "ordered choice". In particular, longest match breaks the linear time guarantee of ordered choice and so makes the parsing vastly more expensive. You should change |
to /
.
I don't think you can just relicense the grammar from https://docs.python.org/3/license.html#psf-license to Boost.
I don't see a definition for NEWLINE
and some other allcaps tokens (DEDENT
). Maybe use the endOfLine
predefined parser? Have a look at https://github.com/PhilippeSigaud/Pegged/wiki/Predefined-Parsers.
You cannot use D keywords or Phobos identifiers in the grammar. That's probably the cause of your problems (tuple
). https://github.com/PhilippeSigaud/Pegged/wiki/Declaring-a-Grammar#rule-names
Hopefully these tips get you a bit closer.
Interesting example by the way, thanks for contributing.
Off-topic: It would be very cool to use this for a Python to D transpiler, so you could do
mixin(`m = "Hello";`.Python.toD); // Mixin Python code.
writeln(m); // Use it in D.
as I demonstrate here: https://youtu.be/HvunD0ZJqiA?t=1225
@veelo thanks for the tip, after fix things you mentioned, and add TYPE_COMMENT, it worked:
dub test
Generating test runner configuration 'python-test-library' for 'library' (library).
Performing "unittest" build using /project/ldc2-1.28.0-linux-x86_64/bin/ldc2 for x86_64.
pegged 0.4.5+commit.10.ge177c5e: building configuration "default"...
python ~master: building configuration "python-test-library"...
Linking...
Running python-test-library
Python[0, 1]["m"]
+-Python.file[0, 1]["m"]
1 modules passed unittests
Please take another look of this PR, and merge if you see no more problems.
I also added Spacing +Comment from ../dgrammar/src/pegged/examples/dgrammar.d,
however I didn't see Spacing
is ever used in the RHS of any rule:
$ grep Spacing ../dgrammar/src/pegged/examples/dgrammar.d
Spacing <- (space / Comment)*
So I guess Spacing
is also a predefined parser? @veelo can you confirm?
Thanks.
So I guess Spacing is also a predefined parser? @veelo can you confirm?
You can select what pattern is considered as a blank: define a rule called
Spacing
in your grammar. This is the rule that will be called by Pegged to consume spaces in this grammar, when the<
arrow is used. If no user-definedSpacing
is provided, Pegged uses the predefinedspacing
rule that parses blank chars (whitespace, tabulation, etc).
See the link above for more info.
You don't seem to use the <
arrow, so any Spacing
rule will not be used. Using <
would possibly mess with INDENT
, which of course would break the semantics of Python. So you'll probably want to remove the Spacing
rule.
Are you having plans to use this grammar yourself? In that case I can hold off the merge until you have played with it some more, in case you discover issues. Let me know when you want it to be merged.
Thanks for the review.
I have removed the Spacing rule.
And I changed NEWLINE
to have line comments.
I actually have other plans for this python grammar to experiment some new language struct, so it's better to merge it as soon as you see no obvious issues; then the more users play with it (standard Python), the more likely new issues can be discovered.
Alright, thanks for your contribution!
Hi,
I tried to add python example, the syntax is taken from:
https://docs.python.org/3/reference/grammar.html
I made minimal modification to use Pegged syntax.
However, the
make test
is still not quite working, and as a Pegged newbie, I cannot figure out how to fix these compilation errors:@veelo if you have time, can you help to fix these errors? then we can have Python here!
Thanks!