Skip to content
Permalink
Browse files

Update C# grammar to get comments ///

  • Loading branch information...
sdpython committed Oct 25, 2018
1 parent 1c9d1fc commit c0886df32d08e1999de4ab8e741c1636469d6427
@@ -39,12 +39,12 @@ def test_csharp_parse(self):
{
public static class world
{
public static double function(double x, doubly y)
public static double function(double x, double y)
{ return x+y ; }
}
public class world_nostatic
{
public virtual double function(double x, doubly y)
public virtual double function(double x, double y)
{ return x+y ; }
}
/// <summary>
@@ -55,7 +55,10 @@ def test_csharp_parse(self):
public int J => myj;
int myj;
public world_nostatic2(int j) { myj = j; }
public override double function(double x, doubly y)
/// <summary>
/// DOCDOC
/// </summary>
public override double function(double x, double y)
{ return base.function(x, y); }
}
}
@@ -66,9 +69,7 @@ def test_csharp_parse(self):
tree = parser.compilation_unit()
st = get_tree_string(tree, parser)
self.assertNotEmpty(st)
help(tree)
help(parser)
print(st)
self.assertIn("/// <summary>", st)


if __name__ == "__main__":
@@ -5,22 +5,22 @@
lexer grammar CSharpLexer;

@lexer::header
{import java.util.Stack;}
{# import java.util.Stack;}

channels { COMMENTS_CHANNEL, DIRECTIVE }

@lexer::members
{private int interpolatedStringLevel;
private Stack<Boolean> interpolatedVerbatiums = new Stack<Boolean>();
private Stack<Integer> curlyLevels = new Stack<Integer>();
private boolean verbatium;
{ self.interpolatedStringLevel = 0;
self.interpolatedVerbatiums = []
self.curlyLevels = []
self.verbatium = False
}

BYTE_ORDER_MARK: '\u00EF\u00BB\u00BF';

SINGLE_LINE_DOC_COMMENT: '///' InputCharacter* -> channel(COMMENTS_CHANNEL);
SINGLE_LINE_DOC_COMMENT: '///' InputCharacter*;
DELIMITED_DOC_COMMENT: '/**' .*? '*/' -> channel(COMMENTS_CHANNEL);
SINGLE_LINE_COMMENT: '//' InputCharacter* -> channel(COMMENTS_CHANNEL);
SINGLE_LINE_COMMENT: '//' InputCharacter*;
DELIMITED_COMMENT: '/*' .*? '*/' -> channel(COMMENTS_CHANNEL);

WHITESPACES: (Whitespace | NewLine)+ -> channel(HIDDEN);
@@ -147,29 +147,24 @@ CHARACTER_LITERAL: '\'' (~['\\\r\n\u0085\u2028\u2029] | Common
REGULAR_STRING: '"' (~["\\\r\n\u0085\u2028\u2029] | CommonCharacter)* '"';
VERBATIUM_STRING: '@"' (~'"' | '""')* '"';
INTERPOLATED_REGULAR_STRING_START: '$"'
{ interpolatedStringLevel++; interpolatedVerbatiums.push(false); verbatium = false; } -> pushMode(INTERPOLATION_STRING);
{ self.interpolatedStringLevel += 1; self.interpolatedVerbatiums.append(False); self.verbatium = False; } -> pushMode(INTERPOLATION_STRING);
INTERPOLATED_VERBATIUM_STRING_START: '$@"'
{ interpolatedStringLevel++; interpolatedVerbatiums.push(true); verbatium = true; } -> pushMode(INTERPOLATION_STRING);
{ self.interpolatedStringLevel += 1; self.interpolatedVerbatiums.push(True); self.verbatium = True; } -> pushMode(INTERPOLATION_STRING);
//B.1.9 Operators And Punctuators
OPEN_BRACE: '{'
{
if (interpolatedStringLevel > 0)
{
curlyLevels.push(curlyLevels.pop() + 1);
}};
if self.interpolatedStringLevel > 0:
self.curlyLevels.push(self.curlyLevels.pop() + 1);
};
CLOSE_BRACE: '}'
{
if (interpolatedStringLevel > 0)
{
curlyLevels.push(curlyLevels.pop() - 1);
if (curlyLevels.peek() == 0)
{
curlyLevels.pop();
skip();
popMode();
}
}
if self.interpolatedStringLevel > 0:
self.curlyLevels.push(curlyLevels.pop() - 1);
if (self.curlyLevels.peek() == 0):
self.curlyLevels.pop();
self.skip();
self.popMode();
};
OPEN_BRACKET: '[';
CLOSE_BRACKET: ']';
@@ -179,24 +174,16 @@ DOT: '.';
COMMA: ',';
COLON: ':'
{
if (interpolatedStringLevel > 0)
{
int ind = 1;
boolean switchToFormatString = true;
while ((char)_input.LA(ind) != '}')
{
if (_input.LA(ind) == ':' || _input.LA(ind) == ')')
{
if (self.interpolatedStringLevel > 0):
ind = 1;
switchToFormatString = true;
while (_input.LA(ind) != '}'):
if (self._input.LA(ind) == ':' or self._input.LA(ind) == ')'):
switchToFormatString = false;
break;
}
ind++;
}
if (switchToFormatString)
{
mode(INTERPOLATION_FORMAT);
}
}
ind += 1
if (switchToFormatString):
self.mode(INTERPOLATION_FORMAT);
};
SEMICOLON: ';';
PLUS: '+';
@@ -239,12 +226,12 @@ OP_LEFT_SHIFT_ASSIGNMENT: '<<=';
mode INTERPOLATION_STRING;
DOUBLE_CURLY_INSIDE: '{{';
OPEN_BRACE_INSIDE: '{' { curlyLevels.push(1); } -> skip, pushMode(DEFAULT_MODE);
REGULAR_CHAR_INSIDE: { !verbatium }? SimpleEscapeSequence;
OPEN_BRACE_INSIDE: '{' { self.curlyLevels.append(1); } -> skip, pushMode(DEFAULT_MODE);
REGULAR_CHAR_INSIDE: { not verbatium }? SimpleEscapeSequence;
VERBATIUM_DOUBLE_QUOTE_INSIDE: { verbatium }? '""';
DOUBLE_QUOTE_INSIDE: '"' { interpolatedStringLevel--; interpolatedVerbatiums.pop();
verbatium = (interpolatedVerbatiums.size() > 0 ? interpolatedVerbatiums.peek() : false); } -> popMode;
REGULAR_STRING_INSIDE: { !verbatium }? ~('{' | '\\' | '"')+;
DOUBLE_QUOTE_INSIDE: '"' { self.interpolatedStringLevel -= 1; self.interpolatedVerbatiums.pop();
self.verbatium = self.interpolatedVerbatiums.peek() if (len(self.interpolatedVerbatiums) > 0) else False; } -> popMode;
REGULAR_STRING_INSIDE: { not verbatium }? ~('{' | '\\' | '"')+;
VERBATIUM_INSIDE_STRING: { verbatium }? ~('{' | '"')+;
mode INTERPOLATION_FORMAT;
@@ -282,7 +269,7 @@ DIRECTIVE_OP_AND: '&&' -> channel(DIREC
DIRECTIVE_OP_OR: '||' -> channel(DIRECTIVE), type(OP_OR);
DIRECTIVE_STRING: '"' ~('"' | [\r\n\u0085\u2028\u2029])* '"' -> channel(DIRECTIVE), type(STRING);
CONDITIONAL_SYMBOL: IdentifierOrKeyword -> channel(DIRECTIVE);
DIRECTIVE_SINGLE_LINE_COMMENT: '//' ~[\r\n\u0085\u2028\u2029]* -> channel(COMMENTS_CHANNEL), type(SINGLE_LINE_COMMENT);
DIRECTIVE_SINGLE_LINE_COMMENT: '//' ~[\r\n\u0085\u2028\u2029]* -> type(SINGLE_LINE_COMMENT);
DIRECTIVE_NEW_LINE: NewLine -> channel(DIRECTIVE), mode(DEFAULT_MODE);
mode DIRECTIVE_TEXT;

0 comments on commit c0886df

Please sign in to comment.
You can’t perform that action at this time.