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

Develop/debug grammar online #65

Open
mingodad opened this issue Jun 26, 2022 · 2 comments
Open

Develop/debug grammar online #65

mingodad opened this issue Jun 26, 2022 · 2 comments

Comments

@mingodad
Copy link

With the grammar shown bellow that's based on https://github.com/jcoglan/canopy/blob/main/src/meta_grammar.peg adapted to be accepted by https://github.com/yhirose/cpp-peglib and it's online playground https://yhirose.github.io/cpp-peglib/ we can develop/debug a canopy grammar with instant feedback.

Copy and paste the grammar shown bellow on the Grammar editor at https://yhirose.github.io/cpp-peglib/ then copy and paste you canopy grammar on the Source code editor and click on the button Parse in the upper right corner.

# From https://github.com/jcoglan/canopy/blob/main/src/meta_grammar.peg
# grammar Canopy.MetaGrammar

# ==============================================================================

grammar               <-  _* grammar_name (_* rule)+ _*  #%grammar

grammar_name          <-  'grammar' ":"? _+ object_identifier

rule                  <-  identifier assignment parsing_expression #%rule

assignment            <-  _+ "<-" _+

# ==============================================================================

~_                     <-  [ \t\n\r] / comment

comment               <-  "#" [^\n]*

object_identifier     <-  <identifier ("." identifier)*>

identifier            <-  <[a-zA-Z_] [a-zA-Z0-9_]*>

integer               <-  <[1-9] [0-9]*>

# ==============================================================================

parsing_expression    <-  choice
                       /  choice_part

choice_part           <-  action_expression
                       /  typed_expression
                       /  sequence
                       /  sequence_element

sequence_element      <-  predicated_atom
                       /  repeated_atom
                       /  maybe_atom
                       /  atom

atom                  <-  reference
                       /  terminal
                       /  paren_expression

terminal              <-  literal_string
                       /  ci_string
                       /  char_class
                       /  any_char

# ==============================================================================

action_expression     <-  actionable _+ action_tag #%action

actionable            <-  sequence
                       /  repeated_atom
                       /  maybe_atom
                       /  terminal
                       /  "(" _* actionable _* ")" #%paren_expr

action_tag            <-  "%" identifier

# ==============================================================================

typed_expression      <-  typable _+ type_tag #%extension

typable               <-  sequence
                       /  sequence_element

type_tag              <-  "<" object_identifier ">"

# ==============================================================================

choice                <-  choice_part (_* "/" _* choice_part)+ #%choice

# ==============================================================================

sequence              <-  sequence_part (_+ sequence_part)+ #%sequence

sequence_part         <-  mute? label? sequence_element #%sequence_part

mute                  <-  "@"

label                 <-  identifier ":"

# ==============================================================================

repeated_atom         <-  atom _* quantifier #%repeat

quantifier            <-  "*" / "+" / "{" _* numeric_quantifier _* "}"

numeric_quantifier    <-  integer (_* "," _* integer?)?

# ==============================================================================

paren_expression      <-  "(" _* parsing_expression _* ")" #%paren_expr

predicated_atom       <-  ("&" / "!") _* atom #%predicate

maybe_atom            <-  atom _* "?" #%maybe

reference             <-  identifier !assignment #%reference

literal_string        <-  '"' ("\\" . / [^"])* '"' #%string
                       /  "'" ("\\" . / [^'])* "'" #%string

ci_string             <-  "`" ("\\" . / [^`])* "`" #%ci_string

char_class            <-  "[" "^"? ("\\" . / [^\]])+ "]" #%char_class

any_char              <-  "." #%any_char

@jcoglan
Copy link
Owner

jcoglan commented Jun 27, 2022

Was this intended as a reply to #64, or are you starting a new issue you'd like me to take action on?

@mingodad
Copy link
Author

It's more of an awareness issue and casually it happen that #64 could also benefit form knowing it.

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