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

Add converted for grammar to ebnf #6995

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft

Add converted for grammar to ebnf #6995

wants to merge 11 commits into from

Conversation

dnwpark
Copy link
Contributor

@dnwpark dnwpark commented Mar 5, 2024

No description provided.

@dnwpark dnwpark changed the title Convert grammar to ebnf Add converted for grammar to ebnf Mar 5, 2024
pass


class EBNF_Literal(EBNF_Item):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python style is no underscores in class names. I'd probably just drop the entire EBNF from the name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to separate file. (For now, will move things to tools later)

if count == len(ebnf_productions):
break

# output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best place for this code to live would be as a tool in edb/tools. There are a bunch of examples there, and then we'd run it like edb generate_ebnf or some such.



def expand_iso_ebnf(item: EBNF_Item) -> str:
if isinstance(item, EBNF_Literal):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and a bunch of other places) would be a perfect time to try out python's new pattern matching system (match)

Comment on lines 25 to 26
class EBNF_Item:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using either dataclass or else our own ast mechanism. (Probably dataclass).

You get to skip writing __init__, the attributes and their types are spelled out more cleanly, and you automatically get string formatting.


def to_iso_ebnf(productions: List[EBNF_Production]) -> List[str]:
return [
production.name + ' = ' + expand_iso_ebnf(production.item) + ';'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using f-strings instead of doing string concatenation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Though use your judgement about whether it's actually an improvement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this bit was a bit too short to use formatting.

Comment on lines 300 to 303
def replace_repeatedly(
item: EBNF_Item,
funcs: List[Callable[[EBNF_Item], Tuple[EBNF_Item, bool]]]
) -> EBNF_Item:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't totally consistent about these things, but typically edgedb style for this kind of definition is to have the closing paren lined up with the def and the body indented one step. (This is what black does, among others)

Comment on lines 314 to 315
return replace_repeatedly(
item, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only indent one step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants