001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools.template_engine; 003 004import java.util.Arrays; 005import java.util.HashSet; 006import java.util.Set; 007 008/** 009 * This class converts a template string (stream of characters) into a stream of tokens. 010 * <p> 011 * The result of the tokenization (also called lexical analysis) serves as input for the 012 * parser {@link TemplateParser}. 013 */ 014public class Tokenizer { 015 016 /** 017 * A token for the parser 018 */ 019 public static class Token { 020 private final TokenType type; 021 private final int position; 022 private final String text; 023 024 /** 025 * A token 026 * @param type The token type 027 * @param position The position of the token 028 */ 029 public Token(TokenType type, int position) { 030 this(type, position, null); 031 } 032 033 /** 034 * A token 035 * @param type The token type 036 * @param position The position of the token 037 * @param text The text for the token 038 */ 039 public Token(TokenType type, int position, String text) { 040 this.type = type; 041 this.position = position; 042 this.text = text; 043 } 044 045 public TokenType getType() { 046 return type; 047 } 048 049 public int getPosition() { 050 return position; 051 } 052 053 public String getText() { 054 return text; 055 } 056 057 @Override 058 public String toString() { 059 return type + (text != null ? ' ' + text : ""); 060 } 061 } 062 063 /** 064 * The token type 065 */ 066 public enum TokenType { CONDITION_START, VARIABLE_START, CONTEXT_SWITCH_START, END, PIPE, APOSTROPHE, TEXT, EOF } 067 068 private final Set<Character> specialCharacters = new HashSet<>(Arrays.asList('$', '?', '{', '}', '|', '\'', '!')); 069 070 private final String template; 071 072 private int c; 073 private int index; 074 private Token currentToken; 075 private final StringBuilder text = new StringBuilder(); 076 077 /** 078 * Creates a new {@link Tokenizer} 079 * @param template the template as a user input string 080 */ 081 public Tokenizer(String template) { 082 this.template = template; 083 getChar(); 084 } 085 086 private void getChar() { 087 if (index >= template.length()) { 088 c = -1; 089 } else { 090 c = template.charAt(index++); 091 } 092 } 093 094 /** 095 * Get the next token 096 * @return The next token 097 * @throws ParseError if there is an error getting the next token 098 */ 099 public Token nextToken() throws ParseError { 100 if (currentToken != null) { 101 Token result = currentToken; 102 currentToken = null; 103 return result; 104 } 105 int position = index; 106 107 text.setLength(0); 108 switch (c) { 109 case -1: 110 return new Token(TokenType.EOF, position); 111 case '{': 112 getChar(); 113 return new Token(TokenType.VARIABLE_START, position); 114 case '?': 115 getChar(); 116 if (c == '{') { 117 getChar(); 118 return new Token(TokenType.CONDITION_START, position); 119 } else 120 throw ParseError.unexpectedChar('{', (char) c, position); 121 case '!': 122 getChar(); 123 if (c == '{') { 124 getChar(); 125 return new Token(TokenType.CONTEXT_SWITCH_START, position); 126 } else 127 throw ParseError.unexpectedChar('{', (char) c, position); 128 case '}': 129 getChar(); 130 return new Token(TokenType.END, position); 131 case '|': 132 getChar(); 133 return new Token(TokenType.PIPE, position); 134 case '\'': 135 getChar(); 136 return new Token(TokenType.APOSTROPHE, position); 137 default: 138 while (c != -1 && !specialCharacters.contains((char) c)) { 139 if (c == '\\') { 140 getChar(); 141 if (c == 'n') { 142 c = '\n'; 143 } 144 } 145 text.append((char) c); 146 getChar(); 147 } 148 return new Token(TokenType.TEXT, position, text.toString()); 149 } 150 } 151 152 /** 153 * Look at the next token 154 * @return The next token 155 * @throws ParseError if there is an error getting the next token 156 */ 157 public Token lookAhead() throws ParseError { 158 if (currentToken == null) { 159 currentToken = nextToken(); 160 } 161 return currentToken; 162 } 163 164 /** 165 * Skip until we hit a character 166 * @param lastChar The last character to skip 167 * @return A token with the skipped characters 168 */ 169 public Token skip(char lastChar) { 170 currentToken = null; 171 int position = index; 172 StringBuilder result = new StringBuilder(); 173 while (c != lastChar && c != -1) { 174 if (c == '\\') { 175 getChar(); 176 } 177 result.append((char) c); 178 getChar(); 179 } 180 return new Token(TokenType.TEXT, position, result.toString()); 181 } 182}