question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to write a rule to identify sentence that have number separated by “,” or “-”

See original GitHub issue

Hello, I got a couple questions. I read all the doc on the web site and two blogs abut nearley.js and I’m still stuck with, that seem, a simple problem.

I have a rule targeting a sentence with number separated by “,” and a other rule that number are separated by “-”.

But the rule that target “,” take over on the rule that target “-”.

My grammar:

MAIN ->  RANGESELECTION
        | ACCUMULATIONSELECTION

BUCKET -> [a-zA-Z0-9-]:+ {%     function(data) {
                                     return {
                                         value: data.join('').replace(/,/g,"")
                                     };
                                 } %}

ACCUMULATIONSELECTION -> "select " BUCKET:+ "," {%
                                  function(data){
                                      return  {type: 'rangeSelection', body: data}
                                     } %}


RANGESELECTION -> "select " (BUCKET "-" BUCKET) {%
                function(data){
                    return  {type: 'rangeSelection', body: [data[0],data[2]]}
                   } %}

The result I would like to have is, when a user enter “select 111-222” the return is a object saying that is a range selection and the 2 values (min, max). And when the sentence is “select a1,a2,a3,a4,a5” the return is a object saying that is a accumulation selection and all the value entered.

thanks in advance for the help

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
Seallycommented, Apr 11, 2019
  • The data argument for the post-processors is an array. You want data[0], data[1], and so on, as appropriate. Note that since we’re using a lexer, each of them is a lexer object, so you want to call data[i].value (or data[i].text in some cases, the Moo docs explains the difference) to get the actual value.
  • You may want to “gather” each value in ACCUMULATION_SELECTION into an array and return it. Check how the JSON grammar example handles the arrays. The return value should be the gathered array with all the values you’ll need inside them.
  • Basically, the top level rule’s post-processor should return the object you want, with stuff like data[0] acting as placeholders for every non-terminal that you want to add into the object (keep in mind that within parentheses the object will actually be in something like data[2][0] and not data[2]). You may want to reorganize the rules to make it easier for you to do this.
1reaction
Seallycommented, Apr 11, 2019

When you use a lexer, every string you use needs to be declared in the lexer. So if you have a grammar like:

helloString -> "Hello" __ "World"
__          -> %whitespaces

Then in the lexer, you must declare:

let lexer = moo.compile({
    whitespaces: { match: /\s+/, lineBreaks: true },
    'Hello': 'Hello',
    'World': 'World'
});

It tripped me up the first time too, but if you think about it, it kinda makes sense, since the lexer completely replaces Nearley’s tokenizer, so it has to know them too. This fact is also subtly demonstrated in the JSON grammar example (you’ll notice all the braces and brackets used are declared in the lexer), but can be easy to miss 😃.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rules for Writing Numbers: Know When To Spell Them Out
Spell Numbers That Begin Sentences. Whenever there are numbers at the beginning of a sentence, those numbers should be written out. Sixty children...
Read more >
10 Rules for Writing Numbers and Numerals - Daily Writing Tips
5. Don't start a sentence with a numeral. Make it “Fourscore and seven years ago,” not “4 score and 7 years ago.” ...
Read more >
Hyphens with Numbers
The number twenty-five is always hyphenated regardless of how it is used in a sentence. You are most likely thinking of the rule...
Read more >
Comma Rules | Style and Grammar | Academic Writing
Use commas to separate independent clauses when they are joined by any of these seven coordinating conjunctions: and, but, for, or, nor, so,...
Read more >
Run-On Sentences and Sentence Fragments - Grammar
You can correct a run-on sentence by connecting or separating its parts correctly. There are several easy ways to connect independent clauses.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found