Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CWL parameter reference parsing #163

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions antlr4/CwlEcmaStringLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
lexer grammar CwlEcmaStringLexer;

ANYCHAR: .;
DOLLARPAREN: '$(' -> pushMode(ParenExpr);
LPAREN: '(';
RPAREN: ')';
DOLLARBRACE: '${' -> pushMode(BraceExpr);
LBRACE: '{';
RBRACE: '}';
DOLLARPARENESC: '\\$(';
DOLLARBRACEESC: '\\${';
BACKSLASH: '\\';
BACKSLASHESC: '\\\\';

mode ParenExpr;

EscPart: BACKSLASH ANYCHAR;
SubExprStart: LPAREN -> pushMode(SubExpr);
ExprEnd: RPAREN -> popMode;
ExprPart: ~[)];

mode SubExpr;

SubEscPart: BACKSLASH ANYCHAR -> type(SubExprPart);
SubSubExprStart: LPAREN -> pushMode(SubExpr);
SubExprEnd: RPAREN -> popMode;
SubExprPart: ~[)];

mode BraceExpr;

BraceEscPart: BACKSLASH ANYCHAR -> type(EscPart);
BraceSubExprStart: LBRACE -> pushMode(BraceSubExpr), type(SubExprStart);
BraceExprEnd: RBRACE -> popMode, type(ExprEnd);
BraceExprPart: ~[}] -> type(ExprPart);

mode BraceSubExpr;

BraceSubEscPart: BACKSLASH ANYCHAR -> type(SubExprPart);
BraceSubSubExprStart: LBRACE -> pushMode(BraceSubExpr), type(SubSubExprStart);
BraceSubExprEnd: RBRACE -> popMode, type(SubExprEnd);
BraceSubExprPart: ~[}] -> type(SubExprPart);
45 changes: 45 additions & 0 deletions antlr4/CwlEcmaStringParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
parser grammar CwlEcmaStringParser;

options {
tokenVocab=CwlEcmaStringLexer;
}

sub_sub_expr
: SubSubExprStart sub_expr_part* SubExprEnd
;

sub_expr_part
: SubExprPart
| sub_sub_expr
;

sub_expr
: SubExprStart sub_expr_part* SubExprEnd
;

expr_part
: ExprPart
| EscPart
| sub_expr
;

paren_expr
: DOLLARPAREN expr_part+ ExprEnd
;

brace_expr
: DOLLARBRACE expr_part+ ExprEnd
;

interpolated_string_part
: BACKSLASHESC
| DOLLARPARENESC
| DOLLARBRACEESC
| paren_expr
| brace_expr
| ANYCHAR
;

interpolated_string
: interpolated_string_part+
;
67 changes: 67 additions & 0 deletions antlr4/CwlParameterReferenceLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
lexer grammar CwlParameterReferenceLexer;

DOT: '.';
LBRACKET: '[';
RBRACKET: ']';
DOLLARPAREN: '$(' -> pushMode(ParenExpr);
RPAREN: ')';
DOLLARPARENESC: '\\$(';
BACKSLASH: '\\';
BACKSLASHESC: '\\\\';
ANYCHAR: .;
LBRACKETSINGLEQ: '[\'';
LBRACKETDOUBLEQ: '["';
SINGLEQRBRACKET: '\']';
DOUBLEQRBRACKET: '"]';

mode ParenExpr;

ExprDot: DOT;
ExprSymbol: CompleteSymbol;
ExprSingleQ: LBRACKETSINGLEQ -> pushMode(SingleQString);
ExprDoubleQ: LBRACKETDOUBLEQ -> pushMode(DoubleQString);
ExprIntIndex: LBRACKET -> pushMode(IntIndex);
EndParenExpr: RPAREN -> popMode;

mode SingleQString;

StringIndexEscPart: BACKSLASH ANYCHAR;
EndSingleQ: SINGLEQRBRACKET -> popMode, type(ExprSingleQ);
StringIndexPart: ~[\\']+;
LiteralBackslash: BACKSLASH -> type(StringIndexPart);

mode DoubleQString;

DoubleQEscapedChar: BACKSLASH ANYCHAR -> type(StringIndexEscPart);
EndDoubleQ: DOUBLEQRBRACKET -> popMode, type(ExprDoubleQ);
DoubleQStringIndexPart: ~[\\"]+ -> type(StringIndexPart);
DoubleQLiteralBackslash: BACKSLASH -> type(StringIndexPart);

mode IntIndex;

EndIndex: RBRACKET -> popMode, type(ExprIntIndex);
IntIndexPart: DecimalNumber;

fragment CompleteSymbol
: SymbolStart SymbolFollow*
;

fragment SymbolStart
: [a-zA-Z]
;

fragment SymbolFollow
: [a-zA-Z0-9_]+
;

fragment DecimalNumber
: DecimalDigit+
;

fragment DecimalDigit
: [0-9]
;

fragment HexDigit
: [0-9a-fA-F]
;
44 changes: 44 additions & 0 deletions antlr4/CwlParameterReferenceParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
parser grammar CwlParameterReferenceParser;

options {
tokenVocab=CwlParameterReferenceLexer;
}

expr_dot_symbol
: ExprDot ExprSymbol
;

int_index
: ExprIntIndex IntIndexPart ExprIntIndex
;

string_index_part
: StringIndexPart
| StringIndexEscPart
;

string_index
: ExprSingleQ string_index_part+ ExprSingleQ
| ExprDoubleQ string_index_part+ ExprDoubleQ
;

expr_segment
: expr_dot_symbol
| int_index
| string_index
;

paren_expr
: DOLLARPAREN ExprSymbol expr_segment* EndParenExpr
;

interpolated_string_part
: BACKSLASHESC
| DOLLARPARENESC
| paren_expr
| ANYCHAR
;

interpolated_string
: interpolated_string_part+
;
131 changes: 131 additions & 0 deletions cwl_utils/parser/v1_2EcmaStringLexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Generated from antlr4/CwlEcmaStringLexer.g4 by ANTLR 4.7.2
from antlr4 import *
from io import StringIO
from typing.io import TextIO
import sys


def serializedATN():
with StringIO() as buf:
buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\24")
buf.write("\u00a0\b\1\b\1\b\1\b\1\b\1\4\2\t\2\4\3\t\3\4\4\t\4\4\5")
buf.write("\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13")
buf.write("\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t")
buf.write("\21\4\22\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26")
buf.write("\4\27\t\27\4\30\t\30\4\31\t\31\4\32\t\32\4\33\t\33\4\34")
buf.write("\t\34\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3\4\3\4\3\5\3\5\3\6")
buf.write("\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\t\3\t\3\n\3")
buf.write("\n\3\n\3\n\3\13\3\13\3\f\3\f\3\f\3\r\3\r\3\r\3\16\3\16")
buf.write("\3\16\3\16\3\17\3\17\3\17\3\17\3\20\3\20\3\21\3\21\3\21")
buf.write("\3\21\3\21\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\24")
buf.write("\3\24\3\25\3\25\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26")
buf.write("\3\27\3\27\3\27\3\27\3\27\3\30\3\30\3\30\3\30\3\31\3\31")
buf.write("\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\33\3\33\3\33")
buf.write("\3\33\3\33\3\34\3\34\3\34\3\34\2\2\35\7\3\t\4\13\5\r\6")
buf.write("\17\7\21\b\23\t\25\n\27\13\31\f\33\r\35\16\37\17!\20#")
buf.write("\21%\2\'\22)\23+\24-\2/\2\61\2\63\2\65\2\67\29\2;\2\7")
buf.write("\2\3\4\5\6\4\3\2++\3\2\177\177\2\u009b\2\7\3\2\2\2\2\t")
buf.write("\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3")
buf.write("\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2")
buf.write("\2\2\2\33\3\2\2\2\3\35\3\2\2\2\3\37\3\2\2\2\3!\3\2\2\2")
buf.write("\3#\3\2\2\2\4%\3\2\2\2\4\'\3\2\2\2\4)\3\2\2\2\4+\3\2\2")
buf.write("\2\5-\3\2\2\2\5/\3\2\2\2\5\61\3\2\2\2\5\63\3\2\2\2\6\65")
buf.write("\3\2\2\2\6\67\3\2\2\2\69\3\2\2\2\6;\3\2\2\2\7=\3\2\2\2")
buf.write("\t?\3\2\2\2\13D\3\2\2\2\rF\3\2\2\2\17H\3\2\2\2\21M\3\2")
buf.write("\2\2\23O\3\2\2\2\25Q\3\2\2\2\27U\3\2\2\2\31Y\3\2\2\2\33")
buf.write("[\3\2\2\2\35^\3\2\2\2\37a\3\2\2\2!e\3\2\2\2#i\3\2\2\2")
buf.write("%k\3\2\2\2\'p\3\2\2\2)t\3\2\2\2+x\3\2\2\2-z\3\2\2\2/\177")
buf.write("\3\2\2\2\61\u0084\3\2\2\2\63\u0089\3\2\2\2\65\u008d\3")
buf.write("\2\2\2\67\u0092\3\2\2\29\u0097\3\2\2\2;\u009c\3\2\2\2")
buf.write("=>\13\2\2\2>\b\3\2\2\2?@\7&\2\2@A\7*\2\2AB\3\2\2\2BC\b")
buf.write("\3\2\2C\n\3\2\2\2DE\7*\2\2E\f\3\2\2\2FG\7+\2\2G\16\3\2")
buf.write("\2\2HI\7&\2\2IJ\7}\2\2JK\3\2\2\2KL\b\6\3\2L\20\3\2\2\2")
buf.write("MN\7}\2\2N\22\3\2\2\2OP\7\177\2\2P\24\3\2\2\2QR\7^\2\2")
buf.write("RS\7&\2\2ST\7*\2\2T\26\3\2\2\2UV\7^\2\2VW\7&\2\2WX\7}")
buf.write("\2\2X\30\3\2\2\2YZ\7^\2\2Z\32\3\2\2\2[\\\7^\2\2\\]\7^")
buf.write("\2\2]\34\3\2\2\2^_\5\31\13\2_`\5\7\2\2`\36\3\2\2\2ab\5")
buf.write("\13\4\2bc\3\2\2\2cd\b\16\4\2d \3\2\2\2ef\5\r\5\2fg\3\2")
buf.write("\2\2gh\b\17\5\2h\"\3\2\2\2ij\n\2\2\2j$\3\2\2\2kl\5\31")
buf.write("\13\2lm\5\7\2\2mn\3\2\2\2no\b\21\6\2o&\3\2\2\2pq\5\13")
buf.write("\4\2qr\3\2\2\2rs\b\22\4\2s(\3\2\2\2tu\5\r\5\2uv\3\2\2")
buf.write("\2vw\b\23\5\2w*\3\2\2\2xy\n\2\2\2y,\3\2\2\2z{\5\31\13")
buf.write("\2{|\5\7\2\2|}\3\2\2\2}~\b\25\7\2~.\3\2\2\2\177\u0080")
buf.write("\5\21\7\2\u0080\u0081\3\2\2\2\u0081\u0082\b\26\b\2\u0082")
buf.write("\u0083\b\26\t\2\u0083\60\3\2\2\2\u0084\u0085\5\23\b\2")
buf.write("\u0085\u0086\3\2\2\2\u0086\u0087\b\27\5\2\u0087\u0088")
buf.write("\b\27\n\2\u0088\62\3\2\2\2\u0089\u008a\n\3\2\2\u008a\u008b")
buf.write("\3\2\2\2\u008b\u008c\b\30\13\2\u008c\64\3\2\2\2\u008d")
buf.write("\u008e\5\31\13\2\u008e\u008f\5\7\2\2\u008f\u0090\3\2\2")
buf.write("\2\u0090\u0091\b\31\6\2\u0091\66\3\2\2\2\u0092\u0093\5")
buf.write("\21\7\2\u0093\u0094\3\2\2\2\u0094\u0095\b\32\b\2\u0095")
buf.write("\u0096\b\32\f\2\u00968\3\2\2\2\u0097\u0098\5\23\b\2\u0098")
buf.write("\u0099\3\2\2\2\u0099\u009a\b\33\5\2\u009a\u009b\b\33\r")
buf.write("\2\u009b:\3\2\2\2\u009c\u009d\n\3\2\2\u009d\u009e\3\2")
buf.write("\2\2\u009e\u009f\b\34\6\2\u009f<\3\2\2\2\7\2\3\4\5\6\16")
buf.write("\7\3\2\7\5\2\7\4\2\6\2\2\t\24\2\t\16\2\7\6\2\t\17\2\t")
buf.write("\20\2\t\21\2\t\22\2\t\23\2")
return buf.getvalue()


class CwlEcmaStringLexer(Lexer):

atn = ATNDeserializer().deserialize(serializedATN())

decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]

ParenExpr = 1
SubExpr = 2
BraceExpr = 3
BraceSubExpr = 4

ANYCHAR = 1
DOLLARPAREN = 2
LPAREN = 3
RPAREN = 4
DOLLARBRACE = 5
LBRACE = 6
RBRACE = 7
DOLLARPARENESC = 8
DOLLARBRACEESC = 9
BACKSLASH = 10
BACKSLASHESC = 11
EscPart = 12
SubExprStart = 13
ExprEnd = 14
ExprPart = 15
SubSubExprStart = 16
SubExprEnd = 17
SubExprPart = 18

channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]

modeNames = [ "DEFAULT_MODE", "ParenExpr", "SubExpr", "BraceExpr", "BraceSubExpr" ]

literalNames = [ "<INVALID>",
"'$('", "'('", "')'", "'${'", "'{'", "'}'", "'\\$('", "'\\${'",
"'\\'", "'\\\\'" ]

symbolicNames = [ "<INVALID>",
"ANYCHAR", "DOLLARPAREN", "LPAREN", "RPAREN", "DOLLARBRACE",
"LBRACE", "RBRACE", "DOLLARPARENESC", "DOLLARBRACEESC", "BACKSLASH",
"BACKSLASHESC", "EscPart", "SubExprStart", "ExprEnd", "ExprPart",
"SubSubExprStart", "SubExprEnd", "SubExprPart" ]

ruleNames = [ "ANYCHAR", "DOLLARPAREN", "LPAREN", "RPAREN", "DOLLARBRACE",
"LBRACE", "RBRACE", "DOLLARPARENESC", "DOLLARBRACEESC",
"BACKSLASH", "BACKSLASHESC", "EscPart", "SubExprStart",
"ExprEnd", "ExprPart", "SubEscPart", "SubSubExprStart",
"SubExprEnd", "SubExprPart", "BraceEscPart", "BraceSubExprStart",
"BraceExprEnd", "BraceExprPart", "BraceSubEscPart", "BraceSubSubExprStart",
"BraceSubExprEnd", "BraceSubExprPart" ]

grammarFileName = "CwlEcmaStringLexer.g4"

def __init__(self, input=None, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.7.2")
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
self._predicates = None


Loading