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

Allow access to different rules in code blocks #669

Open
jcubic opened this issue Oct 15, 2021 · 2 comments
Open

Allow access to different rules in code blocks #669

jcubic opened this issue Oct 15, 2021 · 2 comments

Comments

@jcubic
Copy link

jcubic commented Oct 15, 2021

Issue type

  • Bug Report: no
  • Feature Request: yes
  • Question: no
  • Not an issue: no

Prerequisites

  • Can you reproduce the issue?: yes
  • Did you search the repository issues?: yes
  • Did you check the forums?: yes
  • Did you perform a web search (google, yahoo, etc)?: yes

Description

I want to use rules as an array or have one place where I define my rules.

Steps to Reproduce

I have rules:

adapter_async_strings = "get" / "post" / "ask" { return text(); }
adapter_static_strings = "echo" { return text(); }

I would like to use them in my code when I define a function:

function_definition = _ "def" _ name:variable _ "(" args:(variable _ ","? _)* ")" _  body:statement* _ "end" _ {
    const fn_name = name.name.replace(/\$_/, '');
    console.log(adapter_async_strings); // this is undefined 
    // need to hardcode the array of strings  
    if (["echo", "ask", "get", "post"].includes(fn_name)) {
        const error = new Error(`invalid function name, '${fn_name}' is reseved command`);
        error.location = move_location(location(), 4, fn_name.length + 1);
    	throw error;
    }
    var args = args.map(function(arg) { return arg[0]; });
    return {
        "type": "FunctionDeclaration",
        "id": name,
        "params": args,
        "async": true,
        "body": {
            "type": "BlockStatement",
            "body": body
        }
    };
}

It would be nice if possible to not have to use an array with hardcoded strings to validate and show a proper error message.
but use already defined commands.

Software

  • PEG.js: 0.10.0
  • Node.js: v16.4.2
  • NPM or Yarn: 7.18.1
  • Browser:
  • OS: Fedora GNU/Linux
  • Editor:
@Mingun
Copy link
Contributor

Mingun commented Oct 15, 2021

If that acceptable for you, you can try to move all strings to the per-parse initializer or global initializer (only in Peggy) section:

{{
let methods = ["echo", "ask", "get", "post"];
}}
adapter_strings = word:$[a-z]+ &{ return methods.includes(word); }
function_definition = ... {
  if (methods.includes(...)) { ... }
}

@jcubic
Copy link
Author

jcubic commented Oct 15, 2021

Thanks. this is exactly what I needed.

jcubic added a commit to jcubic/gaiman that referenced this issue Oct 19, 2021
Make the code more DRY by using tip from pegjs/pegjs#669
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

No branches or pull requests

2 participants