File: peg_grammar.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (117 lines) | stat: -rw-r--r-- 5,185 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- tcl -*-
#
# Copyright (c) 2005 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# Parser Generator / Grammar for reading of PE grammars

## Parsing Expression Grammar 'pg::peg::grammar'.

# ### ### ### ######### ######### #########
## Package description

## It provides a single command returning the handle of a
## grammar container in which the grammar 'pg::peg::grammar'
## is stored. The container is usable by a PEG interpreter
## or other packages taking PE grammars.

# ### ### ### ######### ######### #########
## Requisites.
## - PEG container type

package require grammar::peg

namespace eval ::pg::peg::grammar {}

# ### ### ### ######### ######### #########
## API

proc ::pg::peg::grammar {} {
    return $grammar::gr
}

# ### ### ### ######### ######### #########
# ### ### ### ######### ######### #########
## Data and helpers.

namespace eval ::pg::peg::grammar {
    # Grammar container
    variable gr [::grammar::peg gr]
}

proc ::pg::peg::grammar::Start {pe} {
    variable gr
    $gr start $pe
    return
}

proc ::pg::peg::grammar::Define {mode nt pe} {
    variable gr
    $gr nonterminal add  $nt $pe
    $gr nonterminal mode $nt $mode
    return
}

# ### ### ### ######### ######### #########
## Initialization = Grammar definition
    
namespace eval ::pg::peg::grammar {
    Start  {n Grammar}

    Define leaf    ALNUM         {x {t <} {t a} {t l} {t n} {t u} {t m} {t >} {n SPACE}}
    Define leaf    ALPHA         {x {t <} {t a} {t l} {t p} {t h} {t a} {t >} {n SPACE}}
    Define leaf    AND           {x {t &} {n SPACE}}
    Define discard APOSTROPH     {t '}
    Define value   Attribute     {x {/ {n VOID} {n LEAF} {n MATCH}} {n COLON}}
    Define value   Char          {/ {n CharSpecial} {n CharOctalFull} {n CharOctalPart} {n CharUnicode} {n CharUnescaped}}
    Define match   CharOctalFull {x {t \134} {.. 0 2} {.. 0 7} {.. 0 7}}
    Define match   CharOctalPart {x {t \134} {.. 0 7} {? {.. 0 7}}}
    Define match   CharSpecial   {x {t \134} {/ {t n} {t r} {t t} {t '} {t \42} {t \133} {t \135} {t \134}}}
    Define match   CharUnescaped {x {! {t \134}} dot}
    Define match   CharUnicode   {x {t \134} {t u} {n HexDigit} {? {x {n HexDigit} {? {x {n HexDigit} {? {n HexDigit}}}}}}}
    Define value   Class         {x {n OPENB} {* {x {! {n CLOSEB}} {n Range}}} {n CLOSEB} {n SPACE}}
    Define discard CLOSE         {x {t \51} {n SPACE}}
    Define discard CLOSEB        {t \135}
    Define discard COLON         {x {t :} {n SPACE}}
    Define discard COMMENT       {x {t #} {* {x {! {n EOL}} dot}} {n EOL}}
    Define discard DAPOSTROPH    {t \42}
    Define value   Definition    {x {? {n Attribute}} {n Identifier} {n IS} {n Expression} {n SEMICOLON}}
    Define leaf    DOT           {x {t .} {n SPACE}}
    Define discard END           {x {t E} {t N} {t D} {n SPACE}}
    Define discard EOF           {! dot}
    Define discard EOL           {/ {x {t \n} {t \r}} {t \n} {t \r}}
    Define value   Expression    {x {n Sequence} {* {x {n SLASH} {n Sequence}}}}
    Define discard Final         {x {n END} {n SEMICOLON} {n SPACE}}
    Define value   Grammar       {x {n SPACE} {n Header} {+ {n Definition}} {n Final} {n EOF}}
    Define value   Header        {x {n PEG} {n Identifier} {n StartExpr}}
    Define discard HexDigit      {/ {.. 0 9} {.. a f} {.. A F}}
    Define match   Ident         {x {/ {t _} {t :} alpha} {* {/ {t _} {t :} alnum}}}
    Define value   Identifier    {x {n Ident} {n SPACE}}
    Define discard IS            {x {t <} {t -} {n SPACE}}
    Define leaf    LEAF          {x {t l} {t e} {t a} {t f} {n SPACE}}
    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}}}
    Define leaf    MATCH         {x {t m} {t a} {t t} {t c} {t h} {n SPACE}}
    Define leaf    NOT           {x {t !} {n SPACE}}
    Define discard OPEN          {x {t \50} {n SPACE}}
    Define discard OPENB         {t \133}
    Define discard PEG           {x {t P} {t E} {t G} {n SPACE}}
    Define leaf    PLUS          {x {t +} {n SPACE}}
    Define value   Prefix        {x {? {/ {n AND} {n NOT}}} {n Suffix}}
    Define value   Primary       {/ {n ALNUM} {n ALPHA} {n Identifier} {x {n OPEN} {n Expression} {n CLOSE}} {n Literal} {n Class} {n DOT}}
    Define leaf    QUESTION      {x {t ?} {n SPACE}}
    Define value   Range         {/ {x {n Char} {n TO} {n Char}} {n Char}}
    Define discard SEMICOLON     {x {t \73} {n SPACE}}
    Define value   Sequence      {+ {n Prefix}}
    Define discard SLASH         {x {t /} {n SPACE}}
    Define discard SPACE         {* {/ {t \40} {t \t} {n EOL} {n COMMENT}}}
    Define leaf    STAR          {x {t *} {n SPACE}}
    Define value   StartExpr     {x {n OPEN} {n Expression} {n CLOSE}}
    Define value   Suffix        {x {n Primary} {? {/ {n QUESTION} {n STAR} {n PLUS}}}}
    Define discard TO            {t -}
    Define leaf    VOID          {x {t v} {t o} {t i} {t d} {n SPACE}}
}

# ### ### ### ######### ######### #########
## Package Management - Ready

# @sak notprovided pg::peg::grammar
package provide pg::peg::grammar 0.2