canopy icon indicating copy to clipboard operation
canopy copied to clipboard

Develop/debug grammar online

Open mingodad opened this issue 3 years ago • 2 comments
trafficstars

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

mingodad avatar Jun 26 '22 18:06 mingodad

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

jcoglan avatar Jun 27 '22 21:06 jcoglan

It's more of an awareness issue and casually it happen that https://github.com/jcoglan/canopy/issues/64 could also benefit form knowing it.

mingodad avatar Jun 28 '22 06:06 mingodad