Skip to content

Instantly share code, notes, and snippets.

@ikawaha
Created April 7, 2017 13:30
Show Gist options
  • Save ikawaha/12cbd9ba7d75e73ad435a211d598f116 to your computer and use it in GitHub Desktop.
Save ikawaha/12cbd9ba7d75e73ad435a211d598f116 to your computer and use it in GitHub Desktop.
grammar ReStructedText;
file
: section+ EOF
;
section
: section_title section_content+
// | empty_line* -> /* omit line-breaks between paragraphs from AST */
;
section_title
: line underline[$line.length]
{System.out.println("Matched Title: '" + $line.content + "' length: " + $line.length);}
;
section_content
: underline[4]
| paragraph
| section
// | empty_line* -> /* omit line-breaks between paragraphs from AST */
;
underline[int minLength] returns [char symbol]
: first=underline_atom {$symbol=$first.symbol; length++;}
( next=underline_atom {$symbol == $next.symbol}? {length++;} )+
{length >= minLength}?
LINE_BREAK
;
underline_atom returns [char symbol]
: (UNDERSCORE | STAR | PIPE | BACKTICK | COLUMN | SPECIAL_CHAR)
{$symbol = $text.charAt(0);}
;
paragraph
: line+ LINE_BREAK
;
line returns [int length, String content]
@init {
$length = 0;
$content = null;
}
: any_text+
{
$length = $text.length();
$content = $text;
}
LINE_BREAK
;
any_text
: styled_text
| reference
| WS
| ESCAPE_SEQUENCE
| SIMPLE_CHAR
| ANY
;
styled_text
: bold
| italic
| inline_litteral
| interpreted_text
;
bold
: STAR STAR no_star_atom+ STAR STAR
;
italic
: STAR no_star_atom+ STAR
;
no_star_atom
: ~(STAR | LINE_BREAK)
;
interpreted_text
: interpreted_text_class BACKTICK interpreted_text_atoms BACKTICK
| BACKTICK interpreted_text_atoms BACKTICK interpreted_text_class
| BACKTICK interpreted_text_atoms BACKTICK
;
interpreted_text_atoms
: ~BACKTICK+
;
interpreted_text_class
: COLUMN class_atom COLUMN
;
class_atom
: SIMPLE_CHAR+
;
inline_litteral
: BACKTICK BACKTICK inline_litteral_atoms BACKTICK BACKTICK
;
inline_litteral_atoms
: inline_litteral_atom+
;
inline_litteral_atom
: ~BACKTICK
| BACKTICK ~BACKTICK
;
reference
: simple_reference
| long_reference
| substitution_reference
;
simple_reference
: simple_reference_atoms UNDERSCORE
;
simple_reference_atoms
: (SIMPLE_CHAR|ANY)+
;
long_reference
: BACKTICK interpreted_text_atoms BACKTICK UNDERSCORE
;
substitution_reference
: PIPE substitution_reference_atom+ PIPE
;
substitution_reference_atom
: ~PIPE
;
//empty_line
// : LEADING_WS? LINE_BREAK
// ;
/***************
* LEXER RULES *
***************/
UNDERSCORE
: '_'
;
BACKTICK
: '`'
;
STAR
: '*'
;
PIPE
: '|'
;
COLUMN
: ':'
;
SPECIAL_CHAR
: (
'!'
|'"'
|'#'
|'$'
|'%'
|'&'
|'\''
|'('
|')'
|'+'
|','
|'.'
|'/'
|';'
|'<'
|'='
|'>'
|'?'
|'@'
|'['
|'\\'
|']'
|'^'
|'{'
|'}'
|'~'
)
;
ESCAPE_SEQUENCE
: '\\' ('*' | '`' | '|' | '\\')
;
SIMPLE_CHAR
: '-'
| '0'..'9'
| 'A'..'Z'
| 'a'..'z'
;
LINE_BREAK
: '\r'? '\n'
;
WS
: /*{getCharPositionInLine()>0}?=>*/ (' '|'\t'|'\u000C')+
;
//LEADING_WS
//@init {
//int spaces = 0;
//}
// : {getCharPositionInLine() == 0}?=>
// (
// (' ' {spaces++;}|'\u000C' {spaces++;}|'\t' {spaces += 8; spaces -= (spaces \% 8);})+
// {
// // make a string of n spaces where n is column number - 1
// char[] indentation = new char[spaces];
// for (int i=0; i<spaces; i++) {
// indentation[i] = ' ';
// }
// String s = new String(indentation);
// emit(new ClassicToken(LEADING_WS, s));
// }
// )
// ;
ANY
: .
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment