1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.template_engine;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | public class Tokenizer {
|
---|
9 |
|
---|
10 | public static class Token {
|
---|
11 | private final TokenType type;
|
---|
12 | private final int position;
|
---|
13 | private final String text;
|
---|
14 |
|
---|
15 | public Token(TokenType type, int position) {
|
---|
16 | this(type, position, null);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public Token(TokenType type, int position, String text) {
|
---|
20 | this.type = type;
|
---|
21 | this.position = position;
|
---|
22 | this.text = text;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public TokenType getType() {
|
---|
26 | return type;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public int getPosition() {
|
---|
30 | return position;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public String getText() {
|
---|
34 | return text;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public String toString() {
|
---|
39 | return type + (text != null ? ' ' + text : "");
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public enum TokenType { CONDITION_START, VARIABLE_START, CONTEXT_SWITCH_START, END, PIPE, APOSTROPHE, TEXT, EOF }
|
---|
44 |
|
---|
45 | private final Set<Character> specialCharaters = new HashSet<>(Arrays.asList(new Character[] {'$', '?', '{', '}', '|', '\'', '!'}));
|
---|
46 |
|
---|
47 | private final String template;
|
---|
48 |
|
---|
49 | private int c;
|
---|
50 | private int index;
|
---|
51 | private Token currentToken;
|
---|
52 | private final StringBuilder text = new StringBuilder();
|
---|
53 |
|
---|
54 | public Tokenizer(String template) {
|
---|
55 | this.template = template;
|
---|
56 | getChar();
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void getChar() {
|
---|
60 | if (index >= template.length()) {
|
---|
61 | c = -1;
|
---|
62 | } else {
|
---|
63 | c = template.charAt(index++);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public Token nextToken() throws ParseError {
|
---|
68 | if (currentToken != null) {
|
---|
69 | Token result = currentToken;
|
---|
70 | currentToken = null;
|
---|
71 | return result;
|
---|
72 | }
|
---|
73 | int position = index;
|
---|
74 |
|
---|
75 | text.setLength(0);
|
---|
76 | switch (c) {
|
---|
77 | case -1:
|
---|
78 | return new Token(TokenType.EOF, position);
|
---|
79 | case '{':
|
---|
80 | getChar();
|
---|
81 | return new Token(TokenType.VARIABLE_START, position);
|
---|
82 | case '?':
|
---|
83 | getChar();
|
---|
84 | if (c == '{') {
|
---|
85 | getChar();
|
---|
86 | return new Token(TokenType.CONDITION_START, position);
|
---|
87 | } else
|
---|
88 | throw ParseError.unexpectedChar('{', (char) c, position);
|
---|
89 | case '!':
|
---|
90 | getChar();
|
---|
91 | if (c == '{') {
|
---|
92 | getChar();
|
---|
93 | return new Token(TokenType.CONTEXT_SWITCH_START, position);
|
---|
94 | } else
|
---|
95 | throw ParseError.unexpectedChar('{', (char) c, position);
|
---|
96 | case '}':
|
---|
97 | getChar();
|
---|
98 | return new Token(TokenType.END, position);
|
---|
99 | case '|':
|
---|
100 | getChar();
|
---|
101 | return new Token(TokenType.PIPE, position);
|
---|
102 | case '\'':
|
---|
103 | getChar();
|
---|
104 | return new Token(TokenType.APOSTROPHE, position);
|
---|
105 | default:
|
---|
106 | while (c != -1 && !specialCharaters.contains((char) c)) {
|
---|
107 | if (c == '\\') {
|
---|
108 | getChar();
|
---|
109 | if (c == 'n') {
|
---|
110 | c = '\n';
|
---|
111 | }
|
---|
112 | }
|
---|
113 | text.append((char) c);
|
---|
114 | getChar();
|
---|
115 | }
|
---|
116 | return new Token(TokenType.TEXT, position, text.toString());
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public Token lookAhead() throws ParseError {
|
---|
121 | if (currentToken == null) {
|
---|
122 | currentToken = nextToken();
|
---|
123 | }
|
---|
124 | return currentToken;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public Token skip(char lastChar) {
|
---|
128 | currentToken = null;
|
---|
129 | int position = index;
|
---|
130 | StringBuilder result = new StringBuilder();
|
---|
131 | while (c != lastChar && c != -1) {
|
---|
132 | if (c == '\\') {
|
---|
133 | getChar();
|
---|
134 | }
|
---|
135 | result.append((char) c);
|
---|
136 | getChar();
|
---|
137 | }
|
---|
138 | return new Token(TokenType.TEXT, position, result.toString());
|
---|
139 | }
|
---|
140 | }
|
---|