Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active February 17, 2018 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/4bfbfb6b348b0a45d454272896c15416 to your computer and use it in GitHub Desktop.
Save Xliff/4bfbfb6b348b0a45d454272896c15416 to your computer and use it in GitHub Desktop.
Code in Grammars #1

I have the following rule in a complex grammar:

rule predicate {
    :my rule _in_expr { <IN> '(' [ <subselect> | <expr_list> ] ')' }
    [
      <AND> <bit_expr> <BETWEEN> <not>?
      |
      <bit_expr>
    ]
    [
      <_in_expr>
      |
      <not> [
        <_in_expr>
        |
        <LIKE> <simple_expr> <escape>?
        |
        <REGEXP> <bit_expr>
      ]
      |
      [ <SOUNDS> <LIKE> | <REGEXP> ] <bit_expr>
      |
      <LIKE> <simple_expr> <escape>?
    ]?
  }

If I want to improve the readability, can I expand it to multiple lines using the ':' strategy?

rule predicate {
    :my rule _in_expr { 
    :   <IN> '(' [ 
    :     <subselect> 
    :     | 
    :     <expr_list> 
    :   ] ')' 
    :}
    [
      <AND> <bit_expr> <BETWEEN> <not>?
      |
      <bit_expr>
    ]
    [
      <_in_expr>
      |
      <not> [
        <_in_expr>
        |
        <LIKE> <simple_expr> <escape>?
        |
        <REGEXP> <bit_expr>
      ]
      |
      [ <SOUNDS> <LIKE> | <REGEXP> ] <bit_expr>
      |
      <LIKE> <simple_expr> <escape>?
    ]?
  }
@cygx
Copy link

cygx commented Feb 17, 2018

You only need a single :, not one prefixed to each line. You'll be back in the grammar slang after a statement terminator, ie a ; or a closing brace followed by a newline:

my rule foo {
    :my rule bar {
        42
    }
    <bar>
}

say 42 ~~ /<foo>/

@Xliff
Copy link
Author

Xliff commented Feb 17, 2018

Excellent. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment