Skip to content

Commit

Permalink
Preserve the indentation of external code
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8000-0000 committed Apr 6, 2020
1 parent b77ab82 commit 9801a7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/main/antlr/net/signbit/samx/parser/SamX.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ grammar SamX;
package net.signbit.samx.parser;
}

tokens { INDENT, DEDENT, END, INVALID }
tokens { INDENT, DEDENT, END, INVALID, CODE_INDENT }

@lexer::members
{
Expand All @@ -31,6 +31,9 @@ tokens { INDENT, DEDENT, END, INVALID }
private boolean prepareProcessingCode = false;
private boolean processingCode = false;
private int thisIndent = 0;
private int codeIndentBase = 0;
private Token lastToken;
@Override
Expand Down Expand Up @@ -125,6 +128,15 @@ tokens { INDENT, DEDENT, END, INVALID }
}
}

private void addCodeIndent()
{
CommonToken codeIndent = makeToken(SamXParser.CODE_INDENT, java.lang.Integer.toString(thisIndent - codeIndentBase));
codeIndent.setLine(_tokenStartLine);
codeIndent.setCharPositionInLine(_tokenStartCharPositionInLine);
tokens.add(codeIndent);
}

}

SKIP_
Expand All @@ -145,7 +157,7 @@ NEWLINE
)
{
final char[] tokenText = getText().toCharArray();
int thisIndent = 0;
thisIndent = 0;
for (char ch: tokenText)
{
if (ch == ' ')
Expand Down Expand Up @@ -186,6 +198,7 @@ NEWLINE
{
processingCode = true;
prepareProcessingCode = false;
codeIndentBase = thisIndent;
}
}

Expand Down Expand Up @@ -265,10 +278,12 @@ flow : ( text | phrase | localInsert | url | inlineCode )+ ;
paragraph : ( flow NEWLINE )+ NEWLINE ;
recordRow : ( COLSEP flow )+ NEWLINE ;
EXTCODE : (~'\n')+ { processingCode }?;
EXTCODE : (~'\n')+ { processingCode }? { addCodeIndent(); };
CODE_MARKER : '```(' { prepareProcessingCode = true; };
externalCode : CODE_INDENT EXTCODE ;
block :
NAME TYPESEP attribute* description=flow? NEWLINE+ INDENT block+ DEDENT # TypedBlock
| NAME TYPESEP attribute* value=flow NEWLINE # Field
Expand All @@ -280,7 +295,7 @@ block :
| '"""[' text ']' NEWLINE ( INDENT block+ DEDENT ) # CitationBlock
| '>>>(' text ')' attribute* # InsertFragment
| '<<<(' text ')' attribute* # IncludeFile
| CODE_MARKER language=text ')' attribute* NEWLINE+ INDENT (EXTCODE? NEWLINE)+ DEDENT # CodeBlock
| CODE_MARKER language=text ')' attribute* NEWLINE+ INDENT (externalCode? NEWLINE)+ DEDENT # CodeBlock
| NEWLINE # Empty
;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/signbit/samx/PrettyPrinterVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,15 @@ public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx)
builder.append('\n');

indentLevel ++;
for (TerminalNode tn: ctx.EXTCODE())
for (SamXParser.ExternalCodeContext ecc: ctx.externalCode())
{
addIndent(builder);
builder.append(tn.getText());
int codeIndent = Integer.valueOf(ecc.CODE_INDENT().getText());
for (int ii = 0; ii < codeIndent; ++ii)
{
builder.append(' ');
}
builder.append(ecc.EXTCODE().getText());
builder.append('\n');
}
indentLevel --;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/signbit/samx/Tokenize.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static void main(String[] args)
case SamXParser.INVALID:
tokenSymbol = "INVALID";
break;
case SamXParser.CODE_INDENT:
tokenSymbol = "CODE_IDT";
break;
default:
tokenSymbol = \\_(ツ)_/¯";
break;
Expand Down

0 comments on commit 9801a7a

Please sign in to comment.