1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.subst;
15
16 import ch.qos.logback.core.CoreConstants;
17 import ch.qos.logback.core.spi.ScanException;
18
19 import java.util.List;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class Parser {
41
42 static final public String EXPECTING_DATA_AFTER_LEFT_ACCOLADE = "Expecting at least a literal between left accolade and ':-'";
43 static final public String UNMATCHED_RIGHT_ACCOLADE = "Unmatched right accolade '}'";
44
45 final List<Token> tokenList;
46 int pointer = 0;
47
48 public Parser(List<Token> tokenList) {
49 this.tokenList = tokenList;
50 }
51
52 public Node parse() throws ScanException {
53 if (tokenList == null || tokenList.isEmpty())
54 return null;
55 Node node = E();
56
57
58
59 Token t = peekAtCurentToken();
60 if (t != null && t.type == Token.Type.CURLY_RIGHT) {
61 throw new ScanException(UNMATCHED_RIGHT_ACCOLADE);
62 }
63 return node;
64 }
65
66 private Node E() throws ScanException {
67 Node t = T();
68 if (t == null) {
69 return null;
70 }
71 Node eOpt = Eopt();
72 if (eOpt != null) {
73 t.append(eOpt);
74 }
75 return t;
76 }
77
78
79 private Node Eopt() throws ScanException {
80 Token next = peekAtCurentToken();
81 if (next == null) {
82 return null;
83 } else {
84 return E();
85 }
86 }
87
88
89 private Node T() throws ScanException {
90 Token t = peekAtCurentToken();
91 if(t == null) {
92 return null;
93 }
94 switch (t.type) {
95 case LITERAL:
96 advanceTokenPointer();
97 return makeNewLiteralNode(t.payload);
98 case CURLY_LEFT:
99 advanceTokenPointer();
100 Node innerNode = C();
101 Token right = peekAtCurentToken();
102 expectCurlyRight(right);
103 advanceTokenPointer();
104 Node curlyLeft = makeNewLiteralNode(CoreConstants.LEFT_ACCOLADE);
105 curlyLeft.append(innerNode);
106 curlyLeft.append(makeNewLiteralNode(CoreConstants.RIGHT_ACCOLADE));
107 return curlyLeft;
108 case START:
109 advanceTokenPointer();
110 Node v = V();
111 Token w = peekAtCurentToken();
112 expectCurlyRight(w);
113 advanceTokenPointer();
114 return v;
115 default:
116 return null;
117 }
118 }
119
120 private Node makeNewLiteralNode(String s) {
121 return new Node(Node.Type.LITERAL, s);
122 }
123
124
125 private Node V() throws ScanException {
126 Node e = E();
127 Node variable = new Node(Node.Type.VARIABLE, e);
128 Token t = peekAtCurentToken();
129 if (isDefaultToken(t)) {
130 advanceTokenPointer();
131 Node def = Eopt();
132 if(def != null) {
133 variable.defaultPart = def;
134 } else {
135 variable.defaultPart = makeNewLiteralNode(CoreConstants.EMPTY_STRING);
136 }
137 }
138 return variable;
139 }
140
141
142
143 private Node C() throws ScanException {
144 Node e0 = E();
145 Token t = peekAtCurentToken();
146 if (isDefaultToken(t)) {
147 advanceTokenPointer();
148 Node literal = makeNewLiteralNode(CoreConstants.DEFAULT_VALUE_SEPARATOR);
149 if(e0 == null) {
150 throw new ScanException(EXPECTING_DATA_AFTER_LEFT_ACCOLADE);
151 }
152 e0.append(literal);
153 Node e1 = E();
154 e0.append(e1);
155 }
156 return e0;
157 }
158
159 private boolean isDefaultToken(Token t) {
160 return t != null && t.type == Token.Type.DEFAULT;
161 }
162
163 void advanceTokenPointer() {
164 pointer++;
165 }
166
167 void expectNotNull(Token t, String expected) {
168 if (t == null) {
169 throw new IllegalArgumentException("All tokens consumed but was expecting \"" + expected + "\"");
170 }
171 }
172
173 void expectCurlyRight(Token t) throws ScanException {
174 expectNotNull(t, "}");
175 if (t.type != Token.Type.CURLY_RIGHT) {
176 throw new ScanException("Expecting }");
177 }
178 }
179
180 Token peekAtCurentToken() {
181 if (pointer < tokenList.size()) {
182 return tokenList.get(pointer);
183 }
184 return null;
185 }
186
187 }