update metagrammar
[sbp.git] / tests / tibdoc.g
index 38104af..b3b107e 100644 (file)
@@ -1,4 +1,3 @@
-x::="x"
 // interactions between !=> and &~ mean that I need to rethink the chartage
 // indentation styling...
 // literal blocks [[need to ignore bracing]] double-colon style?
@@ -13,76 +12,133 @@ x::="x"
 // image
 // figures
 
+// "reference-style" links
+//
+//   this[1] is fun
+//
+//   [1] http://...
+//
+
+// nonbreaking text?
+// degree: 15^o
+// Arrows: <- -> => <= <->
+
+// textblocks:
+//  - attention, caution, danger, error, hint, important, note, tip, warning
+// definition
+// sidebar
+// figure-with-caption
+// epigraph (end-of-chapter note)
+// compound paragraph (??)
+// csv-table?
+// table of contents
+// header, footer
+// #include
+
+// simple macros (#define) (\define)
+
+// table representation
+//  
+//  \table
+//     a bbb c
+//     ddd   e
+//
+//     [a] ...
+//     [b] ...
+//     [c] ...
+
 // FIXME: these have to go at the top so they have their dropAll bit set before PreSequence.build...
-ws      !::= w**
-w       !::=  [\r\n\ ]
-nw       ::= ~[\r\n\ ]
+w          = " " | "\n" | "\r"
+ws         =  "()":: w**
+//           |  "()":: w** "#" (~[\n])* "\n" ws
+nw         = ~[\r\n\ ]
 
 //////////////////////////////////////////////////////////////////////////////
 
-s                 ::= {Doc} => top
-
-Doc               ::= Header Body                         /ws => doc
-Header            ::= "header" { kv */ ws }               /ws => header
-Body              ::= Section*/ws                             => body
-Section           ::= SectionHeader Paragraph*/ws         /ws => section
-SectionHeader     ::= "==" SectionHeaderBody "=="
-SectionHeaderBody ::=  "=" SectionHeaderBody "="
-                    >      ws text ws
-
-kv         ::= word "=" text /ws => kv1
-
-num !::= [0-9]++
-Paragraph  ::= { "\"\"" ws text }        => "blockquote"
-             > { "*" " " ws text }       => "ul"
-             > { "#" " " ws text }       => "ol"
-             > { num " " ws text }       => "ol"
-             > { "---" "-"* }            => "hr"
-             > { text }                  => "p"
-
-text       ::= item => "t"
-itemx      ::= ws item => "w" | ()
-item       ::= blockquote => "b"
-             > pre         itemx => "a"
-             > structured  itemx => "1"
-             > structuredx itemx => "2"
-             > styled      itemx => "3"
-             > qtext       itemx => "4"
-             > alphanum++  itemx => "5"
-             > symbol      itemx => "6"
-//             > sym++       itemx => []
-             > Paragraph   itemx => "7"
-
-symbol     ::= sym++
-
-blockquote ::= "\"\"" text "\"\""        => "blockquote"
-             | "\"\"" block              => "blockquote"
+s                   = Doc
+
+Doc                 = head:Header ws! body:Body
+Header              = { "\\header" { KeyVal */ ws } /ws }
+Body                = { Section } */ws
+Section             = SectionHeader ws! Paragraph*
+SectionHeader       = "==" SectionHeaderBody "=="
+SectionHeaderBody   =  "=" SectionHeaderBody "="
+                    >      ws! text ws!
+
+sp       = " "**
+blank    = sp! "\n" sp! "\n" ws!
+
+KeyVal       = key:bareword "=" val:text /ws
+wp           = w++
+num          = [0-9]++
+
+Paragraph   = { Blockquote:: "\"\" "    text }
+            > HR::         { "---" "-"*      }
+            >         { OL:: (text &~ (text! ws! {oli}+)) ws! {oli}+  }
+            > P::          { text          }
+
+onums        = nums (". "|") ")!
+any          = ~[]*
+
+uli          =  "* "         (ws! text &~ any (oli|uli)!)
+oli          = onums!        (ws! text &~ any (oli|uli)!)
+
+text         = Item
+
+Item*/ws     =
+               blockquote
+//             > ^"#" ws! { ~[]* }
+             > Verbatim
+             > InlineGrammar
+             > link                       
+             > structured                 
+             > styled                     
+             > (Chars:: alphanum++)       
+             > Quotes:: "\"" text "\""         
+             > (Symbol:: sym++)           
+             > { Block:: text }
+
+word = Chars:: bareword
+
+blockquote = "adsfafewag"
+//blockquote   = Blockquote:: "\"\"" (block | text "\"\"")
              
-qtext      ::= "\"" text "\""            => "quoted"
-pre        ::= "[verbatim]" { ~[]+ } /ws => "verbatim"   // FIXME doesn't work
-
-styled     ::= "__" text "__"      => ul
-             | "((" text "))"      => footnote
-             | ( "[[" text "]]"      => tt
-               >  "[" word "]"       => citation
-               )
-             | "!!" text "!!"      => strikethrough
-             | "^^" (word|block)   => superscript
-             | ",," (word|block)   => subscript
-             | "\\sc" block        => smallcap
-             | "**" text "**"      => bold
-             | "!" (word|block)    => keyword
-             >  "*" text "*"       => it
-
-block ::= { text }
-structured ::= { text }    "->" (url|email) => link
-              //> alphanum++ "->" (url|email) => link
-structuredx ::= glyph
+Verbatim      = "[verbatim]" ws! { (~[])++ }
+
+#import meta.g as meta
+InlineGrammar = "\grammar"   ws! { meta.Grammar }
+
+styled       = Underline::     "__" text "__"      
+             | Footnote::      "((" text "))"      
+             | TT::            "[[" text "]]"
+             | Citation::       "[" word "]"
+             | Strikethrough:: "!!" text "!!"      
+             | Superscript::   "^^" (word|block)   
+             | Subscript::     ",," (word|block)   
+             | Smallcap::      "\\sc" block        
+             | Bold::           "++" text "++"
+             | Keyword::         "!" (word|block)    
+             | Italic::         "**" text "**"
+
+block         = { text }
+
+link          = text:({ text }|word)  "->" ws! href:href
+href          = url | email | {href}
+
+structured    = command & "\\" ([a-zA-Z0-9]++)! block?
+              > glyph
               > email
               > url
 
-glyph      ::= "(r)" | "(c)" | "(tm)" | "--"  // euro symbol?
-             | "\\today" -> ~[a-z] => today
+glyph        = euro::     "(e)"
+             | r::        "(r)"
+             | c::        "(c)"
+             | tm::       "(tm)"
+             | emdash::   "--"
+             | ellipses:: "..."
+             | cent::     "\\cent"
+
+command      = "\\" [a-z]++
 
 
 // URLs //////////////////////////////////////////////////////////////////////////////
@@ -91,44 +147,54 @@ glyph      ::= "(r)" | "(c)" | "(tm)" | "--"  // euro symbol?
 // subtypes of url (ftp, etc) as conjunctions, but the "master pattern"
 // only gets parsed once
 
-urlpath    ::= urlchar*
-username   ::= [a-zA-Z0-9;/?:&=$\-_.+]++
-password   ::= [a-zA-Z0-9;/?:&=$\-_.+]++
-urlchar    ::= [a-zA-Z0-9;/?:&=$\-_.+@]
-             | "%" [0-9] [0-9]       => "%"
-url        ::= "mailto" ":"   email
-             > method "://" url_login? host (":" port)? ("/" urlpath)?     => "url"
-url_login  ::= username (":" password) "@" => "login"
-method     ::= [+\-.a-z0-9]+ 
-port       ::= [0-9]+
-
-domain     ::= (part +/ ".") -> ~"."
-part       ::= [a-zA-Z0-9\-]++    // interesting use of boolean grammars
+urlpath      = urlchar* -> ~urlv           // this ~urlv should be handled by url! bug!
+username     = [a-zA-Z0-9;/?:&=$\-_.+]++
+password     = [a-zA-Z0-9;/?:&=$\-_.+]++
+urlc         = [a-zA-Z0-9;/?:&=$\-_.+@]
+urlv         = urlc | [%]
+urlchar      = urlc
+             | urlescape:: "%" [0-9] [0-9]
+url          = Mailto:: "mailto" ":"   email -> ~urlv
+             > URL::
+                  method:method 
+                  "://"
+                  login:url_login?
+                  host:host
+                  port:(":" nums)?
+                  path:("/" urlpath)?
+                  ref:("#" urlpath)?
+                     -> ~(urlv|[\#])
+url_login    = Login:: username:username password:(":" password) "@"
+method       = [+\-.a-z0-9]+
+domain       = (part +/ ".") -> ~"."
+part         = [a-zA-Z0-9\-]++
+// interesting use of boolean grammars
 //            &~ ([\-0-9] ~[]* | ~[]* [\-0-9])
 
-email      ::= username "@" host -> ~[.] => emailaddr
-host       ::= [0-9]+ "." [0-9]+ "." [0-9]+ "." [0-9]+ => "ip"
-             | domain
+email        = user:username "@" host:host -> ~[.]
+nums         = [0-9]++
+host         = IP::  nums "." nums "." nums "." nums
+             | DNS:: domain
 
 
 
 // Tokens ///////////////////////////////////////////////////////////////////
 
-word     ::= alphanum++
+bareword   = alphanum++
            | quoted
 
-quoted   ::= "\"" ((~[\"\\] | escaped)+) "\""
-           | "\"\"" => ""
-escaped  ::= "\\n" => "\n"
-           | "\\r" => "\r"
+quoted     = "\"" ((~[\"\\] | escaped)+) "\""
+           | "\"\"":: "\"\""
+escaped    = lf:: "\\n"
+           | cr:: "\\r"
            | "\\" ~[nr]
 
 
 // Chars ///////////////////////////////////////////////////////////////
 
-alpha    ::= [a-zA-Z]
-//num      ::= [0-9]
-alphanum ::= [a-zA-Z0-9]
-sym      ::= ~[a-zA-Z0-9\ \r\n=\">]
+alpha      = [a-zA-Z]
+alphanum   = [a-zA-Z0-9]
+sym        = ~[a-zA-Z0-9\ \r\n=\">]
+//sym        = [,()]