/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

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

%token titleitem "!!!";
%token subtitleitem "!!";
%token subsubtitleitem "!";

%token emitem "''";

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

%token lparen "\(";
%token rparen "\)";

%token noteitem "___";

%token strongitem "__";

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

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

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

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

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

%right break "\\\\";

%right hardbreak "(\r(\n?) | \n) ([\ \t]* (\r(\n?) | \n))+";

%token bulleted1item "\*";
%token bulleted2item "\*\*";
%token bulleted3item "\*\*\*";

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

%ignore "[\ \t]+";

%start document;

%%

document 
  : sections
  | subsections
  | subsubsections
  | paragraphs
  | paragraphs sections
  | paragraphs subsections
  | paragraphs subsubsections
  | abreak sections
  | abreak subsections
  | abreak subsubsections
  | abreak paragraphs
  | abreak paragraphs sections
  | abreak paragraphs subsections
  | abreak paragraphs subsubsections
  ;

abreak
  : softbreak
  | hardbreak
  ;

sections
  : sections section
  | section
  ;

subsections
  : subsections subsection
  | subsection
  ;

subsubsections
  : subsubsections subsubsection
  | subsubsection
  ;

section 
  : title paragraphs
  | title paragraphs subsections
  | title subsections
  | title
  ;

subsection
  : subtitle paragraphs
  | subtitle paragraphs subsubsections
  | subtitle subsubsections
  | subtitle
  ;

subsubsection
  : subsubtitle paragraphs
  | subsubtitle
  ;

title
  : titleitem textsequence
  | titleitem textsequence hardbreak
  ;

subtitle
  : subtitleitem textsequence
  | subtitleitem textsequence hardbreak
  ;

subsubtitle
  : subsubtitleitem textsequence
  | subsubtitleitem textsequence hardbreak
  ;

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

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

bulletedlist1 
  : bulletedlist1 bulletedlistitem1 
  | bulletedlistitem1               
  | bulletedlist1 bulletedlist2     
  | bulletedlist2                   
  ;

bulletedlistitem1 
  : bulleted1item textsequence
  ;

bulletedlist2 
  : bulletedlist2 bulletedlistitem2 
  | bulletedlistitem2               
  | bulletedlist2 bulletedlist3     
  | bulletedlist3                   
  ; 

bulletedlistitem2 
  : bulleted2item textsequence
  ;

bulletedlist3 
  : bulletedlist3 bulletedlistitem3 
  | bulletedlistitem3               
  ; 

bulletedlistitem3 
  : bulleted3item 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
  | tablerows
  | tablerows softbreak
  ;

tablehead
  : tablehead tabletitle
  | tabletitle
  ; 

tabletitle
  : tabletitleitem textblock
  ;

tablerows
  : tablerows softbreak tablecolumns
  | tablecolumns
  ;

tablecolumns
  : tablecolumns tablecolumn
  | tablecolumn
  ;

tablecolumn
  : tablecolumnitem textblock
  ;
  
textsequence 
  : textsequence textblock softbreak
  | textsequence textblock 
  | textblock softbreak  
  | textblock
  | textsequence textblock break
  | textblock break
  ; 

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

emblock 
  : emitem text emitem
  ;

strongblock 
  : strongitem text strongitem
  ;

codeblock 
  : codeopenitem text codecloseitem
  ;

noteblock
  : noteitem notetitle textsequence noteitem
  | noteitem notetitle textsequence abreak noteitem
  | noteitem notetitle textsequence bulletedlist1 noteitem
  ;
  
notetitle
  : lparen text rparen
  ;
  
/*
deflistblock
  : defliststart text deflistnext
  ;
*/
