001package org.hl7.fhir.utilities.json.parser; 002 003import java.io.IOException; 004import java.util.ArrayList; 005import java.util.List; 006import java.util.Stack; 007 008import org.hl7.fhir.utilities.Utilities; 009import org.hl7.fhir.utilities.json.model.JsonComment; 010import org.hl7.fhir.utilities.json.model.JsonElement; 011import org.hl7.fhir.utilities.json.model.JsonLocationData; 012 013public class JsonLexer { 014 public static class State { 015 private String name; 016 private boolean isProp; 017 public State(String name, boolean isProp) { 018 super(); 019 this.name = name; 020 this.isProp = isProp; 021 } 022 public String getName() { 023 return name; 024 } 025 public boolean isProp() { 026 return isProp; 027 } 028 } 029 030 public enum TokenType { 031 Open, Close, String, Number, Colon, Comma, OpenArray, CloseArray, Eof, Null, Boolean; 032 033 boolean isValueType() { 034 return this == Open || this == String || this == Number || this == OpenArray || this == Boolean || this == Null; 035 } 036 } 037 038 private String source; 039 private int cursor; 040 private String peek; 041 private String value; 042 private TokenType type; 043 private Stack<State> states = new Stack<State>(); 044 private JsonLocationData lastLocationBWS; 045 private JsonLocationData lastLocationAWS; 046 private JsonLocationData location; 047 private StringBuilder b = new StringBuilder(); 048 private boolean allowComments; 049 private boolean allowUnquotedStrings; 050 private List<JsonComment> comments = new ArrayList<>(); 051 private boolean isUnquoted; 052 private String sourceName; 053 054 private final Stack<String> parenthesis = new Stack<>(); 055 056 public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException { 057 this.source = source; 058 this.allowComments = allowComments; 059 this.allowUnquotedStrings = allowUnquotedStrings; 060 cursor = -1; 061 location = new JsonLocationData(line+1, 1); 062 start(); 063 } 064 065 private boolean more() { 066 return peek != null || cursor < source.length(); 067 } 068 069 private String getNext(int length) throws IOException { 070 String result = ""; 071 if (peek != null) { 072 if (peek.length() > length) { 073 result = peek.substring(0, length); 074 peek = peek.substring(length); 075 } else { 076 result = peek; 077 peek = null; 078 } 079 } 080 if (result.length() < length) { 081 int len = length - result.length(); 082 if (cursor > source.length() - len) 083 throw error("Attempt to read past end of source"); 084 result = result + source.substring(cursor+1, cursor+len+1); 085 cursor = cursor + len; 086 } 087 for (char ch : result.toCharArray()) 088 if (ch == '\n') 089 location.newLine(); 090 else 091 location.incCol(); 092 return result; 093 } 094 095 private char getNextChar() throws IOException { 096 char ch; 097 if (peek != null) { 098 ch = peek.charAt(0); 099 peek = peek.length() == 1 ? null : peek.substring(1); 100 } else { 101 cursor++; 102 if (cursor >= source.length()) { 103 ch = 0; 104 } else { 105 ch = source.charAt(cursor); 106 } 107 } 108 if (ch == '\n') { 109 location.newLine(); 110 } else { 111 location.incCol(); 112 } 113 return ch; 114 } 115 116 private void push(char ch){ 117 peek = peek == null ? String.valueOf(ch) : String.valueOf(ch)+peek; 118 location.back(); 119 } 120 121 public IOException error(String msg) { 122 return new IOException("Error parsing JSON source: "+msg+" at Line "+Integer.toString(location.getLine())+" (path=["+path()+"])"+(sourceName == null ? "" : " from '"+sourceName+"'")); 123 } 124 125 private String path() { 126 if (states.empty()) 127 return value; 128 else { 129 String result = ""; 130 for (State s : states) 131 result = result + '/'+ s.getName(); 132 result = result + value; 133 return result; 134 } 135 } 136 137 public void start() throws IOException { 138 // char ch = getNextChar(); 139 // if (ch = '\.uEF') 140 // begin 141 // // skip BOM 142 // getNextChar(); 143 // getNextChar(); 144 // end 145 // else 146 // push(ch); 147 next(); 148 } 149 150 public TokenType getType() { 151 return type; 152 } 153 154 public String getValue() { 155 return value; 156 } 157 158 159 public JsonLocationData getLastLocationBWS() { 160 return lastLocationBWS; 161 } 162 163 public JsonLocationData getLastLocationAWS() { 164 return lastLocationAWS; 165 } 166 167 168 public void next() throws IOException { 169 lastLocationBWS = location.copy(); 170 char ch; 171 do { 172 ch = getNextChar(); 173 if (allowComments && ch == '/') { 174 JsonLocationData start = location.prev(); 175 char ch1 = getNextChar(); 176 if (ch1 == '/') { 177 StringBuilder b = new StringBuilder(); 178 boolean first = true; 179 while (more() && !Utilities.charInSet(ch, '\r', '\n')) { 180 if (first) first = false; else b.append(ch); 181 ch = getNextChar(); 182 } 183 comments.add(new JsonComment(b.toString().trim(), start, location.prev())); 184 } else { 185 push(ch1); 186 } 187 } 188 } while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t')); 189 lastLocationAWS = location.copy().prev(); 190 isUnquoted = false; 191 if (!more()) { 192 type = TokenType.Eof; 193 if(!parenthesis.empty()) { 194 throw error("One or more parenthesis were not closed: " + parenthesis); 195 } 196 } else { 197 switch (ch) { 198 case '{' : 199 type = TokenType.Open; 200 parenthesis.push("{"); 201 break; 202 case '}' : 203 if(parenthesis.empty()) { 204 throw error("Unexpected close marker '}' at end of JSON"); 205 } 206 String par = parenthesis.pop(); 207 if(!par.equals("{")) { 208 throw error("Unexpected close marker '}'. Expected ']'"); 209 } 210 type = TokenType.Close; 211 break; 212 case '"' : 213 type = TokenType.String; 214 b.setLength(0); 215 do { 216 ch = getNextChar(); 217 if (ch == '\\') { 218 ch = getNextChar(); 219 switch (ch) { 220 case '"': b.append('"'); break; 221 case '\'': b.append('\''); break; 222 case '\\': b.append('\\'); break; 223 case '/': b.append('/'); break; 224 case 'n': b.append('\n'); break; 225 case 'r': b.append('\r'); break; 226 case 't': b.append('\t'); break; 227 case 'u': b.append((char) Integer.parseInt(getNext(4), 16)); break; 228 default : 229 throw error("unknown escape sequence: \\"+ch); 230 } 231 ch = ' '; 232 } else if (ch != '"') 233 b.append(ch); 234 } while (more() && (ch != '"')); 235 if (!more()) 236 throw error("Unclosed string"); 237 value = b.toString(); 238 break; 239 case ':' : 240 type = TokenType.Colon; 241 break; 242 case ',' : 243 type = TokenType.Comma; 244 break; 245 case '[' : 246 parenthesis.push("["); 247 type = TokenType.OpenArray; 248 break; 249 case ']' : 250 if(parenthesis.empty()) { 251 throw error("Unexpected close marker ']' at end of JSON"); 252 } 253 par = parenthesis.pop(); 254 if(!par.equals("[")) { 255 throw error("Unexpected close marker ']'. Expected '}'"); 256 } 257 type = TokenType.CloseArray; 258 break; 259 default: 260 if ((ch >= '0' && ch <= '9') || ch == '-') { 261 type = TokenType.Number; 262 b.setLength(0); 263 while (more() && ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') || ch == '+' || ch == 'e' || ch == 'E') { 264 b.append(ch); 265 ch = getNextChar(); 266 } 267 value = b.toString(); 268 push(ch); 269 } else if (Utilities.isAlphabetic(ch) || (ch == '_')) { 270 type = TokenType.String; 271 isUnquoted = true; 272 b.setLength(0); 273 while (more() && (Utilities.isAlphabetic(ch) || Utilities.isDigit(ch) || Utilities.existsInList(ch, '_', '.', '-'))) { 274 b.append(ch); 275 ch = getNextChar(); 276 } 277 value = b.toString(); 278 push(ch); 279 if ("true".equals(value) || "false".equals(value)) { 280 this.type = TokenType.Boolean; 281 isUnquoted = false; 282 } else if ("null".equals(value)) { 283 this.type = TokenType.Null; 284 isUnquoted = false; 285 } else if (!allowUnquotedStrings) { 286 throw error("Unexpected content '"+value+"' in json format"); 287 } 288 } else { 289 throw error("Unexpected content '"+ch+"' in json format"); 290 } 291 } 292 } 293 } 294 295 public String consume(TokenType type) throws IOException { 296 if (this.type != type) 297 throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString()); 298 String result = value; 299 next(); 300 return result; 301 } 302 303 public JsonLocationData getLocation() { 304 return location; 305 } 306 307 public Stack<State> getStates() { 308 return states; 309 } 310 311 public void takeComments(JsonElement child) { 312 if (!comments.isEmpty()) { 313 child.getComments().addAll(comments); 314 comments.clear(); 315 } 316 } 317 318 public boolean isUnquoted() { 319 return isUnquoted; 320 } 321 322 @Override 323 public String toString() { 324 return "JsonLexer [cursor=" + cursor + ", peek=" + peek + ", type=" + type + ", location=" + location.toString() + "]"; 325 } 326 327 public String getSourceName() { 328 return sourceName; 329 } 330 331 public void setSourceName(String sourceName) { 332 this.sourceName = sourceName; 333 } 334 335 336}