1 package com
.intellij
.codeInsight
.template
.impl
;
3 import com
.intellij
.codeInsight
.template
.Expression
;
4 import com
.intellij
.codeInsight
.template
.Macro
;
5 import com
.intellij
.codeInsight
.template
.macro
.MacroFactory
;
6 import com
.intellij
.lexer
.Lexer
;
7 import com
.intellij
.psi
.tree
.IElementType
;
13 private MacroParser() {
16 //-----------------------------------------------------------------------------------
17 public static Expression
parse(String expression
) {
18 if (expression
.length() == 0) {
19 return new ConstantNode("");
21 Lexer lexer
= new MacroLexer();
22 lexer
.start(expression
, 0, expression
.length(), 0);
23 skipWhitespaces(lexer
);
24 return parseMacro(lexer
, expression
);
27 //-----------------------------------------------------------------------------------
28 private static void advance(Lexer lexer
) {
30 skipWhitespaces(lexer
);
33 //-----------------------------------------------------------------------------------
34 private static void skipWhitespaces(Lexer lexer
) {
35 while (lexer
.getTokenType() == MacroTokenType
.WHITE_SPACE
) {
40 //-----------------------------------------------------------------------------------
41 private static String
getString(Lexer lexer
, String expression
) {
42 return expression
.substring(lexer
.getTokenStart(), lexer
.getTokenEnd());
45 //-----------------------------------------------------------------------------------
46 @SuppressWarnings({"HardCodedStringLiteral"})
47 private static Expression
parseMacro(Lexer lexer
, String expression
) {
48 IElementType tokenType
= lexer
.getTokenType();
49 String token
= getString(lexer
, expression
);
50 if (tokenType
== MacroTokenType
.STRING_LITERAL
) {
53 return new ConstantNode(token
.substring(1, token
.length() - 1).replaceAll("\\\\n", "\n").
54 replaceAll("\\\\r", "\r").replaceAll("\\\\t", "\t").replaceAll("\\\\f", "\f").replaceAll("\\\\(.)", "$1"));
57 if (tokenType
!= MacroTokenType
.IDENTIFIER
) {
58 System
.out
.println("Bad macro syntax: Not identifier: " + token
);
60 return new ConstantNode("");
63 Macro macro
= MacroFactory
.createMacro(token
);
65 return parseVariable(lexer
, expression
);
69 MacroCallNode macroCallNode
= new MacroCallNode(macro
);
70 if (lexer
.getTokenType() == null) {
74 if (lexer
.getTokenType() != MacroTokenType
.LPAREN
) {
79 parseParameters(macroCallNode
, lexer
, expression
);
80 if (lexer
.getTokenType() != MacroTokenType
.RPAREN
) {
81 System
.out
.println("Bad macro syntax: ) expected: " + expression
);
87 private static void parseParameters(MacroCallNode macroCallNode
, Lexer lexer
, String expression
) {
88 if (lexer
.getTokenType() != MacroTokenType
.RPAREN
) {
89 while (lexer
.getTokenType() != null) {
90 Expression node
= parseMacro(lexer
, expression
);
91 macroCallNode
.addParameter(node
);
93 if (lexer
.getTokenType() == MacroTokenType
.COMMA
) {
103 private static Expression
parseVariable(Lexer lexer
, String expression
) {
104 String variableName
= getString(lexer
, expression
);
107 if (lexer
.getTokenType() == null) {
108 if (TemplateImpl
.END
.equals(variableName
)) {
109 return new EmptyNode();
112 return new VariableNode(variableName
, null);
115 if (lexer
.getTokenType() != MacroTokenType
.EQ
) {
116 return new VariableNode(variableName
, null);
120 Expression node
= parseMacro(lexer
, expression
);
121 return new VariableNode(variableName
, node
);