lark icon indicating copy to clipboard operation
lark copied to clipboard

Add grammar for ISO 8601 (datetime)

Open aatifsyed opened this issue 4 years ago • 9 comments

Hi Lark maintainers!

This is my first PR to an open source project.
It offers a grammar that covers some of ISO 8601.
I'd like to add some tests with your guidance, and polish this PR until it's of acceptable quality :)

aatifsyed avatar Dec 20 '20 20:12 aatifsyed

This is probably better as a single Terminal (e.g. everything UPPERCASE). (Or at least a few sections as Terminals). Also, this should be added to the example which compiles all grammars, and adding tests would be nice.

MegaIng avatar Dec 20 '20 20:12 MegaIng

Ah, I see now

lark.exceptions.GrammarError: Rules aren't allowed inside terminals (prefixed_week_number in WEEK_DATE)

Why is that? Also, what is the example that you were alluding to?

aatifsyed avatar Dec 20 '20 20:12 aatifsyed

I'm getting an error that I can't quite wrap my head around:

lark.exceptions.GrammarError: Using an undefined rule: NonTerminal('start')

(Edit: looks like I've misunderstood the ?)

aatifsyed avatar Dec 20 '20 20:12 aatifsyed

Rules aren't allowed inside terminals

Terminals, by definition, are the smallest element in the grammar. They are merely a string of characters. They cannot contain rules, which are the grammar equivalent of functions, in the same way that a Python string cannot contain a list (but a list can contain a string).

erezsh avatar Dec 20 '20 21:12 erezsh

Hm, I'm still having trouble with this

I'm getting an error that I can't quite wrap my head around:

lark.exceptions.GrammarError: Using an undefined rule: NonTerminal('start')

What am I missing?

aatifsyed avatar Dec 20 '20 21:12 aatifsyed

Hm, I think I'll need to level up my lark. I'm erroring on just the following, emulating common.lark, and I'm not sure why

%import common.DIGIT

YEAR  : DIGIT DIGIT DIGIT DIGIT

MONTH : "1".."9"
lark.exceptions.GrammarError: Using an undefined rule: NonTerminal('start')

aatifsyed avatar Dec 20 '20 21:12 aatifsyed

Alright, I've found my feet on this again, and actually been able to use it, after getting more familiar with Lark :) A few questions for the maintainers:

  • Where should tests go? Their own file? Or is there somewhere they already fit, as @MegaIng alluded to?
  • Should everything really be a terminal? It might be useful to users to e.g pull out the TIME token from a DATETIME?

aatifsyed avatar Dec 21 '20 18:12 aatifsyed

Heyo 👋 what's the status of this PR? A date could be parsed via a single regex.

* Where should tests go? Their own file? Or is there somewhere they already fit, as @MegaIng alluded to?

AFIAK, tests are in the tests directory. They are written using the unittest framework (personally, I prefer pytest).

* Should everything really be a terminal? It might be useful to users to e.g pull out the `TIME` token from a `DATETIME`?

Hmm, true. Then the datetime should be a rule, combining time and date

ThatXliner avatar Jan 26 '21 22:01 ThatXliner

Sorry, I didn't notice this went unanswered.

Where should tests go?

Probably a new file, test_grammar_lib.py or something like that.

Should everything really be a terminal? It might be useful to users to e.g pull out the TIME token from a DATETIME?

That would still be possible with terminals. The user could do %import date_iso (TIME).

erezsh avatar Jan 26 '21 22:01 erezsh