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