How to access a database of valid terms from within a rule?

60 views
Skip to first unread message

Robert Muetzelfeldt

unread,
Sep 23, 2017, 3:44:02 PM9/23/17
to PEG.js: Parser Generator for JavaScript
I'm working on a grammar for the names of scientific quatities.  Toy example:
   weight of iron
   weight of copper
   weight of tin
   density of iron
   density of copper
   etc

Simple grammar: 
    start = measure _ "of" _ metal
    measure = "weight"/"density"
    metal = "iron"/"copper"/"tin"
    _ = " "

But what I want to do is to store the metals in a simple database, as an array of object literals, e.g.
   var metals = [{name:"iron",colour:"grey"},{name:"copper", colour:"brown"},......]
and have this (a) in the initializer section, or (b) in the rest of my code (preferable, since I'll be using the metals database for other purposes).

How do I adapt the 'metal' rule to handle this?  I presume I need a "predicate", but can't see how to formulate it.

Thanks,
Robert

jas...@snmpstack.com

unread,
Oct 4, 2017, 9:42:46 PM10/4/17
to PEG.js: Parser Generator for JavaScript
I am not quite sure I understand the question but I think you want something like this:

{
var metals = {
      "iron": {colour:"grey", weight:5, density:1.2},
      "copper": {colour:"brown", weight:4, density:1.0},
      "tin": {colour:"gray", weight:2, density:.7}
    };

}
start = rule1+
rule1 = measure:measure _ "of" _ metal:metal rest_of_line? {
return metals[metal][measure];
}
measure = "weight"/"density"
metal = "iron"/"copper"/"tin"

    
rest_of_line
  = a:(!(NL) .)* NL {
    return a;
  }
  

NL = 
    "\r\n" / "\n" 
_ = 

Kris...@yahoo.com

unread,
Dec 19, 2017, 11:31:41 PM12/19/17
to PEG.js: Parser Generator for JavaScript
Is there a way to define the possible values through a variable (instead of hardcoding)?

For example, in the example, there is this rule:
metal = "iron" / "copper" / "tin"

The "iron", "copper" etc. are hardcoded in the rule as explicit strings. Is there a way to supply these values in a variable that is perhaps loaded in JS?

For example,
{
var metals = ["iron", "copper", "tin"]
}
metal = metals //somehow make it use the metals array from the JS code above

Thanks,
GK
http://Cenacle.Company

Guilherme Vieira

unread,
Dec 20, 2017, 1:02:32 PM12/20/17
to kris...@yahoo.com, PEG.js: Parser Generator for JavaScript
Hi. Is this what you want? Hopefully the code is self-explanatory, but if you have any questions, let me know: https://gist.github.com/n2liquid/45fb8e8cc557a9d5d8186b46cda4569c

Atenciosamente / Sincerely,
Guilherme Prá Vieira


--
You received this message because you are subscribed to the Google Groups "PEG.js: Parser Generator for JavaScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pegjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gopalakrishna Palem

unread,
Dec 20, 2017, 10:47:53 PM12/20/17
to Guilherme Vieira, PEG.js: Parser Generator for JavaScript
Cool. Thanks. It looks interesting. Will try that approach.

Wish PegJS supports external includes (so that the JS script could be maintained and loaded from an external file).

GK

--
You received this message because you are subscribed to a topic in the Google Groups "PEG.js: Parser Generator for JavaScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pegjs/YxVMa8wY3Z0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pegjs+un...@googlegroups.com.

Guilherme Vieira

unread,
Dec 21, 2017, 7:57:28 AM12/21/17
to Gopalakrishna Palem, PEG.js: Parser Generator for JavaScript
There are different ways to have dependencies in your grammar, depending on the grammar's output format or the environment its running in.

If you're running your grammar from NodeJS, the grammar's JavaScript should have access to the require function.

If you've compiled your grammar, saved it to a file, and you're requiring it, require calls inside it should resolve dependencies relative to the directory where the compiled grammar is stored.

If you've compiled your grammar using the PEG.js API with output option set to "parser" (the default; see the docs), require calls inside the grammar should resolve dependencies relative to the directory of the module that compiled the grammar.

If you're compiling the grammar to run in the browser, to be loaded via <script> tag, you can always use dependencies found in global variables.

Lastly, you can always combine the --dependency (CLI) or dependencies (JS) option with the --format (CLI) or format (JS) option to have PEG.js automatically generate require calls, import statements, etc, depending on the module output format you choose (amd, commonjs, or umd; bare and globals formats don't generate dependency import statements).

Docs:
Source:
Happy hacking :)

Atenciosamente / Sincerely,
Guilherme Prá Vieira

To unsubscribe from this group and all its topics, send an email to pegjs+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages