Skip to content

Commit

Permalink
Refactor names and change title handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ReMinoer committed Apr 28, 2017
1 parent 3421887 commit 886371f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 133 deletions.
20 changes: 2 additions & 18 deletions DashLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,12 @@ DASH_MEDIA_MODE_INNER_BRACKET: VOID? '<';
DASH_MEDIA_MODE_INNER_CLOSE: VOID? '<...>' WS? -> popMode;

mode Header;
HEADER_TITLE_1: '-';
HEADER_TITLE_2: '--';
HEADER_TITLE_3: '---';
HEADER_TITLE_4: '----';
HEADER_TITLE_5: '-----';
HEADER_TITLE_6: '------';
HEADER_TITLE_7: '-------';
HEADER_TITLE_8: '--------';
HEADER_TITLE_9: '---------';
HEADER_TITLE: '-'+;
HEADER_CONTENT: (WS? ('<' HEADER_CONTENT WS? '>' | ~('>'|' '|'\t'|'\n'|'\r')+))+;
HEADER_CLOSE: WS? '>' WS? -> popMode;

mode HeaderMode;
HEADER_MODE_TITLE_1: '-';
HEADER_MODE_TITLE_2: '--';
HEADER_MODE_TITLE_3: '---';
HEADER_MODE_TITLE_4: '----';
HEADER_MODE_TITLE_5: '-----';
HEADER_MODE_TITLE_6: '------';
HEADER_MODE_TITLE_7: '-------';
HEADER_MODE_TITLE_8: '--------';
HEADER_MODE_TITLE_9: '---------';
HEADER_MODE_TITLE: '-'+;
HEADER_MODE_CONTENT: (WS? ('<' HEADER_MODE_CONTENT WS? '>' | ~('>'|' '|'\t'|'\n'|'\r')+))+;
HEADER_MODE_CLOSE: WS? '>' WS? '>' WS? -> popMode;

Expand Down
188 changes: 73 additions & 115 deletions DashParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ private int WhiteSpaceSize(string whiteSpace) {

parse:
NEWLINE*
( document_title NEWLINE* )*
( documentTitle NEWLINE* )*
(
( comment_block
| comment_inline
| header_mode
| extension_mode
| dash_extension_mode
( commentBlock
| commentInline
| headerMode
| extensionMode
| dashExtensionMode
| MODE_CLOSE
| ( redirection
| note
Expand All @@ -60,13 +60,13 @@ parse:
WS? EOF
;

document_title: HEADER_OPEN HEADER_CLOSE NEWLINE? line;
paragraph: header line | NEWLINE (header NEWLINE)? ((list | line) (NEWLINE (header_mode | list | line))*)? MODE_CLOSE?;
documentTitle: HEADER_OPEN HEADER_CLOSE NEWLINE? line;
paragraph: (titleHeader | header) line | NEWLINE ((titleHeader | header) NEWLINE)? ((list | line) (NEWLINE (headerMode | list | line))*)? MODE_CLOSE?;

line:
( WS?
( comment_block
| comment_inline
( commentBlock
| commentInline
| reference
| media
| bold
Expand All @@ -75,18 +75,18 @@ line:
| strikethrough
| emphasis
| link
| direct_link
| directLink
| address
| text
| others
)
)+
;

emphasis_line:
emphasisLine:
( WS?
( comment_block
| comment_inline
( commentBlock
| commentInline
| reference
| media
| bold
Expand All @@ -95,18 +95,18 @@ emphasis_line:
| strikethrough
| emphasis
| link
| direct_link
| directLink
| address
| text
| emphasis_others
| emphasisOthers
)
)+
;

link_line:
linkLine:
( WS?
( comment_block
| comment_inline
( commentBlock
| commentInline
| reference
| media
| bold
Expand All @@ -115,10 +115,10 @@ link_line:
| strikethrough
| emphasis
| link
| direct_link
| directLink
| address
| text
| link_others
| linkOthers
)
)+
;
Expand Down Expand Up @@ -170,7 +170,7 @@ others:
);

// Without SELECTION_CLOSE
emphasis_others:
emphasisOthers:
( COMMENT_BLOCK_OPEN
| COMMENT_INLINE_OPEN
| MEDIA_OPEN
Expand Down Expand Up @@ -214,7 +214,7 @@ emphasis_others:
);

// Without LINK_MIDDLE
link_others:
linkOthers:
( COMMENT_BLOCK_OPEN
| COMMENT_INLINE_OPEN
| MEDIA_OPEN
Expand Down Expand Up @@ -257,93 +257,51 @@ link_others:
| ADDRESS_SEPARATOR
);

header:
HEADER_OPEN
( HEADER_TITLE_1
| HEADER_TITLE_2
| HEADER_TITLE_3
| HEADER_TITLE_4
| HEADER_TITLE_5
| HEADER_TITLE_6
| HEADER_TITLE_7
| HEADER_TITLE_8
| HEADER_TITLE_9
| header_content
)
HEADER_CLOSE;

header_content:
header: HEADER_OPEN headerContent? HEADER_CLOSE;
titleHeader: HEADER_OPEN HEADER_TITLE HEADER_CLOSE;
headerContent:
( HEADER_CONTENT
| HEADER_MODE_TITLE_1
| HEADER_MODE_TITLE_2
| HEADER_MODE_TITLE_3
| HEADER_MODE_TITLE_4
| HEADER_MODE_TITLE_5
| HEADER_MODE_TITLE_6
| HEADER_MODE_TITLE_7
| HEADER_MODE_TITLE_8
| HEADER_MODE_TITLE_9
| HEADER_TITLE
)+;

header_mode:
HEADER_MODE_OPEN
( HEADER_MODE_TITLE_1
| HEADER_MODE_TITLE_2
| HEADER_MODE_TITLE_3
| HEADER_MODE_TITLE_4
| HEADER_MODE_TITLE_5
| HEADER_MODE_TITLE_6
| HEADER_MODE_TITLE_7
| HEADER_MODE_TITLE_8
| HEADER_MODE_TITLE_9
| header_mode_content
)?
HEADER_MODE_CLOSE;

header_mode_content:
headerMode: HEADER_MODE_OPEN headerModeContent? HEADER_MODE_CLOSE;
titleHeaderMode: HEADER_MODE_OPEN HEADER_MODE_TITLE HEADER_MODE_CLOSE;
headerModeContent:
( HEADER_MODE_CONTENT
| HEADER_MODE_TITLE_1
| HEADER_MODE_TITLE_2
| HEADER_MODE_TITLE_3
| HEADER_MODE_TITLE_4
| HEADER_MODE_TITLE_5
| HEADER_MODE_TITLE_6
| HEADER_MODE_TITLE_7
| HEADER_MODE_TITLE_8
| HEADER_MODE_TITLE_9
| HEADER_MODE_TITLE
)+;

bold: BOLD_OPEN emphasis_line SELECTION_CLOSE;
italic: ITALIC_OPEN emphasis_line SELECTION_CLOSE;
underline: UNDERLINE_OPEN emphasis_line SELECTION_CLOSE;
strikethrough: STRIKETHROUGH_OPEN emphasis_line SELECTION_CLOSE;
emphasis: HEADER_OPEN header_content HEADER_CLOSE SELECTION_OPEN emphasis_line SELECTION_CLOSE;
bold: BOLD_OPEN emphasisLine SELECTION_CLOSE;
italic: ITALIC_OPEN emphasisLine SELECTION_CLOSE;
underline: UNDERLINE_OPEN emphasisLine SELECTION_CLOSE;
strikethrough: STRIKETHROUGH_OPEN emphasisLine SELECTION_CLOSE;
emphasis: HEADER_OPEN headerContent HEADER_CLOSE SELECTION_OPEN emphasisLine SELECTION_CLOSE;

link: SELECTION_OPEN link_line LINK_MIDDLE link_content LINK_CLOSE;
link_content: LINK_CONTENT;
direct_link: DIRECT_LINK_OPEN direct_link_content DIRECT_LINK_CLOSE;
direct_link_content: DIRECT_LINK_CONTENT;
link: SELECTION_OPEN linkLine LINK_MIDDLE linkContent LINK_CLOSE;
linkContent: LINK_CONTENT;
directLink: DIRECT_LINK_OPEN directLinkContent DIRECT_LINK_CLOSE;
directLinkContent: DIRECT_LINK_CONTENT;

address: ADDRESS_OPEN address_content (ADDRESS_SEPARATOR address_content)* ADDRESS_CLOSE;
address_content: ADDRESS_CONTENT;
address: ADDRESS_OPEN addressContent (ADDRESS_SEPARATOR addressContent)* ADDRESS_CLOSE;
addressContent: ADDRESS_CONTENT;

reference: SELECTION_OPEN link_line LINK_MIDDLE reference_number LINK_CLOSE;
reference_number: REFERENCE_NUMBER;
reference: SELECTION_OPEN linkLine LINK_MIDDLE referenceNumber LINK_CLOSE;
referenceNumber: REFERENCE_NUMBER;

note: ADDRESS_OPEN note_number ADDRESS_CLOSE line;
redirection: ADDRESS_OPEN note_number ADDRESS_CLOSE DIRECT_LINK_OPEN direct_link_content DIRECT_LINK_CLOSE;
note_number: NOTE_NUMBER;
note: ADDRESS_OPEN noteNumber ADDRESS_CLOSE line;
redirection: ADDRESS_OPEN noteNumber ADDRESS_CLOSE DIRECT_LINK_OPEN directLinkContent DIRECT_LINK_CLOSE;
noteNumber: NOTE_NUMBER;

media: (EXTENSION_OPEN media_extension (EXTENSION_PLUS | EXTENSION_MINUS)* EXTENSION_CLOSE NEWLINE?)? MEDIA_OPEN media_content? MEDIA_CLOSE;
media_extension: ((EXTENSION_PLUS | EXTENSION_MINUS)* EXTENSION_CONTENT)*;
media_content: (MEDIA_CONTENT | MEDIA_BRACES_OPEN | MEDIA_CLOSE)+;
media: (EXTENSION_OPEN mediaExtension (EXTENSION_PLUS | EXTENSION_MINUS)* EXTENSION_CLOSE NEWLINE?)? MEDIA_OPEN mediaContent? MEDIA_CLOSE;
mediaExtension: ((EXTENSION_PLUS | EXTENSION_MINUS)* EXTENSION_CONTENT)*;
mediaContent: (MEDIA_CONTENT | MEDIA_BRACES_OPEN | MEDIA_CLOSE)+;

extension_mode: EXTENSION_MODE_OPEN extension_mode_extension (EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CLOSE extension_mode_content? MEDIA_MODE_CLOSE;
extension_mode_extension: ((EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CONTENT)*;
extension_mode_content: (MEDIA_MODE_CONTENT | MEDIA_MODE_BRACKET)+;
extensionMode: EXTENSION_MODE_OPEN extensionModeExtension (EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CLOSE extensionModeContent? MEDIA_MODE_CLOSE;
extensionModeExtension: ((EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CONTENT)*;
extensionModeContent: (MEDIA_MODE_CONTENT | MEDIA_MODE_BRACKET)+;

dash_extension_mode: EXTENSION_MODE_OPEN EXTENSION_MODE_DASH (DASH_EXTENSION_PLUS | DASH_EXTENSION_MINUS)* DASH_EXTENSION_CLOSE dash_extension_mode_content? DASH_MEDIA_MODE_CLOSE;
dash_extension_mode_content:
dashExtensionMode: EXTENSION_MODE_OPEN EXTENSION_MODE_DASH (DASH_EXTENSION_PLUS | DASH_EXTENSION_MINUS)* DASH_EXTENSION_CLOSE dashExtensionModeContent? DASH_MEDIA_MODE_CLOSE;
dashExtensionModeContent:
( DASH_MEDIA_MODE_CONTENT
| DASH_MEDIA_MODE_BRACKET
| DASH_MEDIA_MODE_CLOSE
Expand All @@ -355,23 +313,23 @@ dash_extension_mode_content:
)+
;

comment_inline: COMMENT_INLINE_OPEN comment_inline_content COMMENT_INLINE_CLOSE;
comment_inline_content: COMMENT_INLINE_CONTENT;
comment_block: COMMENT_BLOCK_OPEN comment_block_content COMMENT_BLOCK_CLOSE;
comment_block_content: COMMENT_BLOCK_CONTENT;
commentInline: COMMENT_INLINE_OPEN commentInlineContent COMMENT_INLINE_CLOSE;
commentInlineContent: COMMENT_INLINE_CONTENT;
commentBlock: COMMENT_BLOCK_OPEN commentBlockContent COMMENT_BLOCK_CLOSE;
commentBlockContent: COMMENT_BLOCK_CONTENT;

list locals [int depth = 0]:
(
tabs=WS
/*<>*/ { $depth = whiteSpaceSize($tabs.getText()); }
/*<csharp> { $depth = WhiteSpaceSize($tabs.text); } */
)?
( LIST_NUMBER WS? list_ordered[$depth]
| LIST_BULLET WS? list_bulleted[$depth]
( LIST_NUMBER WS? listOrdered[$depth]
| LIST_BULLET WS? listBulleted[$depth]
)
;

list_bulleted [int currentDepth] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
listBulleted [int currentDepth] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
line
(
NEWLINE
Expand All @@ -386,13 +344,13 @@ list_bulleted [int currentDepth] locals /*<>*/ [int depth, boolean ordered = fal
)
WS?
( { $ordered }?
subo=sublist_ordered[$depth]
subo=sublistOrdered[$depth]
(
{ $subo.returnDepth >= 0 }?
line
)?
| { !$ordered && $depth > $currentDepth }?
subb=sublist_bulleted[$depth]
subb=sublistBulleted[$depth]
(
{ $subb.returnDepth >= 0 }?
line
Expand All @@ -403,7 +361,7 @@ list_bulleted [int currentDepth] locals /*<>*/ [int depth, boolean ordered = fal
)*
;

sublist_bulleted [int currentDepth] returns [int returnDepth = -1] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
sublistBulleted [int currentDepth] returns [int returnDepth = -1] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
line
(
{ $returnDepth < 0 }?
Expand All @@ -418,15 +376,15 @@ sublist_bulleted [int currentDepth] returns [int returnDepth = -1] locals /*<>*/
| LIST_BULLET
)
( { $ordered && $depth >= $currentDepth }?
subo=sublist_ordered[$depth]
subo=sublistOrdered[$depth]
(
{ $subo.returnDepth >= $currentDepth }?
line
| { $subo.returnDepth < $currentDepth }?
{ $returnDepth = $subo.returnDepth; }
)
| { !$ordered && $depth > $currentDepth }?
subb=sublist_bulleted[$depth]
subb=sublistBulleted[$depth]
(
{ $subb.returnDepth >= $currentDepth }?
line
Expand All @@ -441,7 +399,7 @@ sublist_bulleted [int currentDepth] returns [int returnDepth = -1] locals /*<>*/
)*
;

list_ordered [int currentDepth] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
listOrdered [int currentDepth] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
line
(
NEWLINE
Expand All @@ -455,13 +413,13 @@ list_ordered [int currentDepth] locals /*<>*/ [int depth, boolean ordered = fals
| LIST_BULLET
)
( { !$ordered }?
subb=sublist_bulleted[$depth]
subb=sublistBulleted[$depth]
(
{ $subb.returnDepth >= 0 }?
line
)?
| { $ordered && $depth > $currentDepth }?
subo=sublist_ordered[$depth]
subo=sublistOrdered[$depth]
(
{ $subo.returnDepth >= 0 }?
line
Expand All @@ -472,7 +430,7 @@ list_ordered [int currentDepth] locals /*<>*/ [int depth, boolean ordered = fals
)*
;

sublist_ordered [int currentDepth] returns [int returnDepth = -1] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
sublistOrdered [int currentDepth] returns [int returnDepth = -1] locals /*<>*/ [int depth, boolean ordered = false] /* <csharp> [int depth, bool ordered = false] */:
line
(
{ $returnDepth < 0 }?
Expand All @@ -487,15 +445,15 @@ sublist_ordered [int currentDepth] returns [int returnDepth = -1] locals /*<>*/
| LIST_BULLET
)
( { !$ordered && $depth >= $currentDepth }?
subb=sublist_bulleted[$depth]
subb=sublistBulleted[$depth]
(
{ $subb.returnDepth >= $currentDepth }?
line
| { $subb.returnDepth < $currentDepth }?
{ $returnDepth = $subb.returnDepth; }
)
| { $ordered && $depth > $currentDepth }?
subo=sublist_ordered[$depth]
subo=sublistOrdered[$depth]
(
{ $subo.returnDepth >= $currentDepth }?
line
Expand Down

0 comments on commit 886371f

Please sign in to comment.