Skip to content

Commit

Permalink
Implement tabulation handling in media mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ReMinoer committed Sep 28, 2017
1 parent ae45e12 commit 76b7f0c
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 305 deletions.
27 changes: 18 additions & 9 deletions DashLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ EXTENSION_MODE_MINUS: WS? '-';
EXTENSION_MODE_PLUS: WS? '+';

mode MediaMode;
MEDIA_MODE_CONTENT: (VOID? ~('<'|' '|'\t'|'\n'|'\r')+)+;
MEDIA_MODE_BRACKET: VOID? '<';
MEDIA_MODE_SPACE: ' ';
MEDIA_MODE_TAB: '\t';
MEDIA_MODE_NEWLINE: NEWLINE;
MEDIA_MODE_CONTENT: ~('<'|' '|'\t'|'\n'|'\r')+;
MEDIA_MODE_BRACKET: '<';
MEDIA_MODE_CLOSE: VOID? '<...>' WS? -> popMode, popMode;

mode DashExtension;
Expand All @@ -73,16 +76,22 @@ DASH_EXTENSION_PLUS: WS? '+';
DASH_EXTENSION_MINUS: WS? '-';

mode DashMediaMode;
DASH_MEDIA_MODE_DASH: WS? '<' WS? '<' (~[>] | '>' ~[>])* '>' WS? '>' (WS? (('\r'? '\n' | '\r') | EOF))* -> pushMode(DashMediaModeInner);
DASH_MEDIA_MODE_CONTENT: (VOID? ~('<'|' '|'\t'|'\n'|'\r')+)+;
DASH_MEDIA_MODE_BRACKET: VOID? '<';
DASH_MEDIA_MODE_SPACE: ' ';
DASH_MEDIA_MODE_TAB: '\t';
DASH_MEDIA_MODE_NEWLINE: NEWLINE;
DASH_MEDIA_MODE_DASH: '<' WS? '<' (~[>] | '>' ~[>])* '>' WS? '>' -> pushMode(DashMediaModeInner);
DASH_MEDIA_MODE_CONTENT: ~('<'|' '|'\t'|'\n'|'\r')+;
DASH_MEDIA_MODE_BRACKET: '<';
DASH_MEDIA_MODE_CLOSE: VOID? '<...>' WS? -> popMode, popMode, popMode;

mode DashMediaModeInner;
DASH_MEDIA_MODE_INNER_DASH: WS? '<' WS? '<' (~[>] | '>' ~[>])* '>' WS? '>' (WS? (('\r'? '\n' | '\r') | EOF))* -> pushMode(DashMediaModeInner);
DASH_MEDIA_MODE_INNER_CONTENT: (VOID? ~('<'|' '|'\t'|'\n'|'\r')+)+;
DASH_MEDIA_MODE_INNER_BRACKET: VOID? '<';
DASH_MEDIA_MODE_INNER_CLOSE: VOID? '<...>' WS? -> popMode;
DASH_MEDIA_MODE_INNER_SPACE: ' ';
DASH_MEDIA_MODE_INNER_TAB: '\t';
DASH_MEDIA_MODE_INNER_NEWLINE: NEWLINE;
DASH_MEDIA_MODE_INNER_DASH: '<' WS? '<' (~[>] | '>' ~[>])* '>' WS? '>' -> pushMode(DashMediaModeInner);
DASH_MEDIA_MODE_INNER_CONTENT: ~('<'|' '|'\t'|'\n'|'\r')+;
DASH_MEDIA_MODE_INNER_BRACKET: '<';
DASH_MEDIA_MODE_INNER_CLOSE: '<...>' WS? -> popMode;

mode Header;
HEADER_TITLE: '-'+;
Expand Down
68 changes: 58 additions & 10 deletions DashParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@ options { tokenVocab=DashLexer; }
/*<>*/
@members
{
private int whiteSpaceSize(String whiteSpace) {
public static final int TabSize = 4;
public static int whiteSpaceSize(String whiteSpace) {
int size = 0;
for (char c: whiteSpace.toCharArray())
switch (c) {
case ' ': size++; break;
case '\t': size += 4; break;
case '\t': size += TabSize; break;
}
return size;
}
}
/*<csharp>
@members
{
private int WhiteSpaceSize(string whiteSpace) {
public const int TabSize = 4;
public static int WhiteSpaceSize(string whiteSpace) {
int size = 0;
foreach (char c in whiteSpace)
switch (c) {
case ' ': size++; break;
case '\t': size += 4; break;
case '\t': size += TabSize; break;
}
return size;
}
Expand Down Expand Up @@ -300,21 +304,65 @@ media: (EXTENSION_OPEN mediaExtension (EXTENSION_PLUS | EXTENSION_MINUS)* EXTENS
mediaExtension: ((EXTENSION_PLUS | EXTENSION_MINUS)* EXTENSION_CONTENT)*;
mediaContent: (MEDIA_CONTENT | MEDIA_BRACES_OPEN | MEDIA_CLOSE)+;

extensionMode: EXTENSION_MODE_OPEN extensionModeExtension (EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CLOSE extensionModeContent? MEDIA_MODE_CLOSE;
extensionMode: EXTENSION_MODE_OPEN extensionModeExtension (EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CLOSE extensionModeContent MEDIA_MODE_NEWLINE* (MEDIA_MODE_SPACE | MEDIA_MODE_TAB)* MEDIA_MODE_CLOSE;
extensionModeExtension: ((EXTENSION_MODE_PLUS | EXTENSION_MODE_MINUS)* EXTENSION_MODE_CONTENT)*;
extensionModeContent: (MEDIA_MODE_CONTENT | MEDIA_MODE_BRACKET)+;

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
extensionModeContent locals [int tabSize = 0, int lineTabSize = 0]:
( MEDIA_MODE_SPACE { $tabSize++; }
| MEDIA_MODE_TAB { $tabSize += TabSize; }
)*
extensionModeLine
(
MEDIA_MODE_NEWLINE
( { $lineTabSize < $tabSize }?
( MEDIA_MODE_SPACE { $lineTabSize++; }
| MEDIA_MODE_TAB { $lineTabSize += TabSize; }
)
)*
extensionModeLine { $lineTabSize = 0; }
)*
;

extensionModeLine:
( MEDIA_MODE_SPACE
| MEDIA_MODE_TAB
| MEDIA_MODE_CONTENT
| MEDIA_MODE_BRACKET
)*
;

dashExtensionMode: EXTENSION_MODE_OPEN EXTENSION_MODE_DASH (DASH_EXTENSION_PLUS | DASH_EXTENSION_MINUS)* DASH_EXTENSION_CLOSE dashExtensionModeContent DASH_MEDIA_MODE_CLOSE;

dashExtensionModeContent locals [int tabSize = 0, int lineTabSize = 0]:
( (DASH_MEDIA_MODE_SPACE | DASH_MEDIA_MODE_INNER_SPACE) { $tabSize++; }
| (DASH_MEDIA_MODE_TAB | DASH_MEDIA_MODE_INNER_TAB) { $tabSize += TabSize; }
)*
dashExtensionModeLine
(
(DASH_MEDIA_MODE_NEWLINE | DASH_MEDIA_MODE_INNER_NEWLINE)
( { $lineTabSize < $tabSize }?
( (DASH_MEDIA_MODE_SPACE | DASH_MEDIA_MODE_INNER_SPACE) { $lineTabSize++; }
| (DASH_MEDIA_MODE_TAB | DASH_MEDIA_MODE_INNER_TAB) { $lineTabSize += TabSize; }
)
)*
dashExtensionModeLine { $lineTabSize = 0; }
)*
;

dashExtensionModeLine:
( DASH_MEDIA_MODE_SPACE
| DASH_MEDIA_MODE_TAB
| DASH_MEDIA_MODE_CONTENT
| DASH_MEDIA_MODE_BRACKET
| DASH_MEDIA_MODE_CLOSE
| DASH_MEDIA_MODE_DASH
| DASH_MEDIA_MODE_INNER_SPACE
| DASH_MEDIA_MODE_INNER_TAB
| DASH_MEDIA_MODE_INNER_CONTENT
| DASH_MEDIA_MODE_INNER_BRACKET
| DASH_MEDIA_MODE_INNER_CLOSE
| DASH_MEDIA_MODE_INNER_DASH
)+
)*
;

commentInline: COMMENT_INLINE_OPEN commentInlineContent COMMENT_INLINE_CLOSE;
Expand Down
Loading

0 comments on commit 76b7f0c

Please sign in to comment.