1# -*- tcl -*- 2# 3# Copyright (c) 2005 by Andreas Kupries <andreas_kupries@users.sourceforge.net> 4# Parser Generator / Grammar for reading of PE grammars 5 6## Parsing Expression Grammar 'pg::peg::grammar'. 7 8# ### ### ### ######### ######### ######### 9## Package description 10 11## It provides a single command returning the handle of a 12## grammar container in which the grammar 'pg::peg::grammar' 13## is stored. The container is usable by a PEG interpreter 14## or other packages taking PE grammars. 15 16# ### ### ### ######### ######### ######### 17## Requisites. 18## - PEG container type 19 20package require grammar::peg 21 22namespace eval ::pg::peg::grammar {} 23 24# ### ### ### ######### ######### ######### 25## API 26 27proc ::pg::peg::grammar {} { 28 return $grammar::gr 29} 30 31# ### ### ### ######### ######### ######### 32# ### ### ### ######### ######### ######### 33## Data and helpers. 34 35namespace eval ::pg::peg::grammar { 36 # Grammar container 37 variable gr [::grammar::peg gr] 38} 39 40proc ::pg::peg::grammar::Start {pe} { 41 variable gr 42 $gr start $pe 43 return 44} 45 46proc ::pg::peg::grammar::Define {mode nt pe} { 47 variable gr 48 $gr nonterminal add $nt $pe 49 $gr nonterminal mode $nt $mode 50 return 51} 52 53# ### ### ### ######### ######### ######### 54## Initialization = Grammar definition 55 56namespace eval ::pg::peg::grammar { 57 Start {n Grammar} 58 59 Define leaf ALNUM {x {t <} {t a} {t l} {t n} {t u} {t m} {t >} {n SPACE}} 60 Define leaf ALPHA {x {t <} {t a} {t l} {t p} {t h} {t a} {t >} {n SPACE}} 61 Define leaf AND {x {t &} {n SPACE}} 62 Define discard APOSTROPH {t '} 63 Define value Attribute {x {/ {n VOID} {n LEAF} {n MATCH}} {n COLON}} 64 Define value Char {/ {n CharSpecial} {n CharOctalFull} {n CharOctalPart} {n CharUnicode} {n CharUnescaped}} 65 Define match CharOctalFull {x {t \134} {.. 0 2} {.. 0 7} {.. 0 7}} 66 Define match CharOctalPart {x {t \134} {.. 0 7} {? {.. 0 7}}} 67 Define match CharSpecial {x {t \134} {/ {t n} {t r} {t t} {t '} {t \42} {t \133} {t \135} {t \134}}} 68 Define match CharUnescaped {x {! {t \134}} dot} 69 Define match CharUnicode {x {t \134} {t u} {n HexDigit} {? {x {n HexDigit} {? {x {n HexDigit} {? {n HexDigit}}}}}}} 70 Define value Class {x {n OPENB} {* {x {! {n CLOSEB}} {n Range}}} {n CLOSEB} {n SPACE}} 71 Define discard CLOSE {x {t \51} {n SPACE}} 72 Define discard CLOSEB {t \135} 73 Define discard COLON {x {t :} {n SPACE}} 74 Define discard COMMENT {x {t #} {* {x {! {n EOL}} dot}} {n EOL}} 75 Define discard DAPOSTROPH {t \42} 76 Define value Definition {x {? {n Attribute}} {n Identifier} {n IS} {n Expression} {n SEMICOLON}} 77 Define leaf DOT {x {t .} {n SPACE}} 78 Define discard END {x {t E} {t N} {t D} {n SPACE}} 79 Define discard EOF {! dot} 80 Define discard EOL {/ {x {t \n} {t \r}} {t \n} {t \r}} 81 Define value Expression {x {n Sequence} {* {x {n SLASH} {n Sequence}}}} 82 Define discard Final {x {n END} {n SEMICOLON} {n SPACE}} 83 Define value Grammar {x {n SPACE} {n Header} {+ {n Definition}} {n Final} {n EOF}} 84 Define value Header {x {n PEG} {n Identifier} {n StartExpr}} 85 Define discard HexDigit {/ {.. 0 9} {.. a f} {.. A F}} 86 Define match Ident {x {/ {t _} {t :} alpha} {* {/ {t _} {t :} alnum}}} 87 Define value Identifier {x {n Ident} {n SPACE}} 88 Define discard IS {x {t <} {t -} {n SPACE}} 89 Define leaf LEAF {x {t l} {t e} {t a} {t f} {n SPACE}} 90 Define value Literal {/ {x {n APOSTROPH} {* {x {! {n APOSTROPH}} {n Char}}} {n APOSTROPH} {n SPACE}} {x {n DAPOSTROPH} {* {x {! {n DAPOSTROPH}} {n Char}}} {n DAPOSTROPH} {n SPACE}}} 91 Define leaf MATCH {x {t m} {t a} {t t} {t c} {t h} {n SPACE}} 92 Define leaf NOT {x {t !} {n SPACE}} 93 Define discard OPEN {x {t \50} {n SPACE}} 94 Define discard OPENB {t \133} 95 Define discard PEG {x {t P} {t E} {t G} {n SPACE}} 96 Define leaf PLUS {x {t +} {n SPACE}} 97 Define value Prefix {x {? {/ {n AND} {n NOT}}} {n Suffix}} 98 Define value Primary {/ {n ALNUM} {n ALPHA} {n Identifier} {x {n OPEN} {n Expression} {n CLOSE}} {n Literal} {n Class} {n DOT}} 99 Define leaf QUESTION {x {t ?} {n SPACE}} 100 Define value Range {/ {x {n Char} {n TO} {n Char}} {n Char}} 101 Define discard SEMICOLON {x {t \73} {n SPACE}} 102 Define value Sequence {+ {n Prefix}} 103 Define discard SLASH {x {t /} {n SPACE}} 104 Define discard SPACE {* {/ {t \40} {t \t} {n EOL} {n COMMENT}}} 105 Define leaf STAR {x {t *} {n SPACE}} 106 Define value StartExpr {x {n OPEN} {n Expression} {n CLOSE}} 107 Define value Suffix {x {n Primary} {? {/ {n QUESTION} {n STAR} {n PLUS}}}} 108 Define discard TO {t -} 109 Define leaf VOID {x {t v} {t o} {t i} {t d} {n SPACE}} 110} 111 112# ### ### ### ######### ######### ######### 113## Package Management - Ready 114 115# @sak notprovided pg::peg::grammar 116package provide pg::peg::grammar 0.1 117 118