
%token line "\-\-\-\- (\-+)";

%token bulleteditem "\*+";

%token numbered1item "#";
%token numbered2item "##";
%token numbered3item "###";

%token tabletitleitem  "\|\|";
%token tablecolumnitem "\|";

%token titleitem "!+";

%token emitem "''";

%token codeopenitem "\{\{";
%token codecloseitem "\}\}";

%token strongitem "__";

%token source "\{\{\{ (\}{0,2}[^\}])* \}\}\}";

%token anchor "\[( [^\[\|\]]* \| )? # [^\[\|\]]* \]";

%token link   "\[( [^\[\|\]]* \| )?   [^\[\|\]]* \]";

%token text  "([^\ \t\n\r\[\{\}\|\*_!#'] | _[^_] | \{[^\{] | \}[^\}] | '[^'] | \[\[)
              ([^    \n\r\[\{\}\|_']     | _[^_] | \{[^\{] | \}[^\}] | '[^'] | \[\[ | \|[^\|\ \t])*";

%right softbreak "\r(\n?) | \n";

%right hardbreak "(\r(\n?) | \n) (\r(\n?) | \n)+";

%ignore "[\ \t]+";

%start document;

%%

document 
  : document section 
  | section
  | paragraphs
  ;

section 
  : title paragraphs
  | title hardbreak paragraphs
  ;

paragraphs  
  : paragraphs paragraph hardbreak
  | paragraphs paragraph 
  | paragraph hardbreak
  | paragraph
  ;

paragraph 
  : bulletedlist
  | numberedlist1
  | textsequence
  | line 
  | source %prec softbreak
  | source softbreak
  | table
  ;

bulletedlist 
  : bulletedlist bulletedlistitem 
  | bulletedlistitem              
  ;

bulletedlistitem 
  : bulleteditem textsequence
  ;

numberedlist1 
  : numberedlist1 numberedlistitem1 
  | numberedlistitem1               
  | numberedlist1 numberedlist2     
  | numberedlist2                   
  ;

numberedlistitem1 
  : numbered1item textsequence
  ;

numberedlist2 
  : numberedlist2 numberedlistitem2 
  | numberedlistitem2               
  | numberedlist2 numberedlist3     
  | numberedlist3                   
  ; 

numberedlistitem2 
  : numbered2item textsequence
  ;

numberedlist3 
  : numberedlist3 numberedlistitem3 
  | numberedlistitem3               
  ; 

numberedlistitem3 
  : numbered3item textsequence
  ;

table
  : tablehead softbreak tablerows
  | tablehead softbreak tablerows softbreak
  ;

tablehead
  : tablehead tabletitle
  | tabletitle
  ; 

tabletitle
  : tabletitleitem textblock
  ;

tablerows
  : tablerows softbreak tablecolumns
  | tablecolumns
  ;

tablecolumns
  : tablecolumns tablecolumn
  | tablecolumn
  ;

tablecolumn
  : tablecolumnitem textblock
  ;
  
title 
  : titleitem textsequence
  ;

textsequence 
  : textsequence textblock softbreak
  | textsequence textblock 
  | textblock softbreak  
  | textblock           
  ; 

textblock 
  : link
  | anchor
  | strongblock                      
  | emblock            
  | text                         
  | codeblock
  ;

emblock 
  : emitem text emitem
  ;

strongblock 
  : strongitem text strongitem
  ;

codeblock 
  : codeopenitem text codecloseitem
  ;

