Giter Club home page Giter Club logo

Comments (4)

Mingun avatar Mingun commented on August 12, 2024

If I correctly understand your needs, you have a two ways to do that:

  • just use choice operator
    decide
      = integer
      / word
      / ...
      ;
    Remember, that order of alternatives is important! If one rule is a prefix of other (for example, = and ==) the longest should be the first
  • you need a some rule that will match your tag and then you can analyze this tag using semantic predicate:
    // returns array [tag_result, rule_result]
    decide = tag:tag (
        &{ return isInteger(tag); } @integer
      / &{ return isWord(tag); } @word
      / ...
    );
    Because probably the tags will uniquely determine the type, the order not so important, but the right order can make grammar faster. Put more expected alternatives first

from peggy.

barsdeveloper avatar barsdeveloper commented on August 12, 2024

Good idea, thanks.
Do you know if I can have in the grammar (I am using CLI to generate my parser at compile time throught npm run build command) a mix of static and dynamic rules?

integer = [0-9]+ // static rule
word = [a-z]i+ // static rule
entry = tag `${some generated rule that makes the grammar}` // dynamic rule generated through javascript code

I understand it is a bit fuzzy but I am new to parser generators. Thanks for the help and patience.

from peggy.

Mingun avatar Mingun commented on August 12, 2024

You cannot have ${some generated rule that makes the grammar} in the grammar itself, but you can generate your grammar before running the CLI. You also could write a plugin that will append necessary rules directly to the AST instead of generating grammar text. If you want, you can even write that plugin in that way, that is would read its input from the same grammar file, so technically your grammar will contain both static and dynamic rules:

{{
  // For example, you does not use top-level initializer and
  // you decide to write some code inside it for your plugin
}}
integer = [0-9]+ // static rule
word = [a-z]i+ // static rule

// dynamic rules will be injected to the AST by plugin, 
// plugin take their input from `ast.topLevelInitializer`

from peggy.

hildjj avatar hildjj commented on August 12, 2024

I'm going to close this. Please reopen if there is a concrete feature to request.

from peggy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.