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  public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException {
055    this.source = source;
056    this.allowComments = allowComments;
057    this.allowUnquotedStrings = allowUnquotedStrings;
058    cursor = -1;
059    location = new JsonLocationData(line+1, 1);  
060    start();
061  }
062
063  private boolean more() {
064    return peek != null || cursor < source.length(); 
065  }
066
067  private String getNext(int length) throws IOException {
068    String result = "";
069    if (peek != null) {
070      if (peek.length() > length) {
071        result = peek.substring(0, length);
072        peek = peek.substring(length);
073      } else {
074        result = peek;
075        peek = null;
076      }
077    }
078    if (result.length() < length) {
079      int len = length - result.length(); 
080      if (cursor > source.length() - len) 
081        throw error("Attempt to read past end of source");
082      result = result + source.substring(cursor+1, cursor+len+1);
083      cursor = cursor + len;
084    }
085    for (char ch : result.toCharArray())
086      if (ch == '\n')
087        location.newLine();
088      else
089        location.incCol();
090    return result;
091  }
092
093  private char getNextChar() throws IOException {
094    char ch;
095    if (peek != null) {
096      ch = peek.charAt(0);
097      peek = peek.length() == 1 ? null : peek.substring(1);
098    } else {
099      cursor++;
100      if (cursor >= source.length()) {
101        ch = 0;
102      } else {
103        ch = source.charAt(cursor);
104      }
105    }
106    if (ch == '\n') {
107      location.newLine();
108    } else {
109      location.incCol();
110    }
111    return ch;
112  }
113
114  private void push(char ch){
115    peek = peek == null ? String.valueOf(ch) : String.valueOf(ch)+peek;
116    location.back();
117  }
118
119  public IOException error(String msg) {
120    return new IOException("Error parsing JSON source: "+msg+" at Line "+Integer.toString(location.getLine())+" (path=["+path()+"])"+(sourceName == null ? "" : " from '"+sourceName+"'"));
121  }
122
123  private String path() {
124    if (states.empty())
125      return value;
126    else {
127      String result = "";
128      for (State s : states) 
129        result = result + '/'+ s.getName();
130      result = result + value;
131      return result;
132    }
133  }
134
135  public void start() throws IOException {
136    //      char ch = getNextChar();
137    //      if (ch = '\.uEF')
138    //      begin
139    //        // skip BOM
140    //        getNextChar();
141    //        getNextChar();
142    //      end
143    //      else
144    //        push(ch);
145    next();
146  }
147
148  public TokenType getType() {
149    return type;
150  }
151
152  public String getValue() {
153    return value;
154  }
155
156
157  public JsonLocationData getLastLocationBWS() {
158    return lastLocationBWS;
159  }
160
161  public JsonLocationData getLastLocationAWS() {
162    return lastLocationAWS;
163  }
164
165  public void next() throws IOException {
166    lastLocationBWS = location.copy();
167    char ch;
168    do {
169      ch = getNextChar();
170      if (allowComments && ch == '/') {
171        JsonLocationData start = location.prev();
172        char ch1 = getNextChar();
173        if (ch1 == '/') {
174          StringBuilder b = new StringBuilder();
175          boolean first = true;
176          while (more() && !Utilities.charInSet(ch, '\r', '\n')) {
177            if (first) first = false; else b.append(ch);
178            ch = getNextChar();
179          }
180          comments.add(new JsonComment(b.toString().trim(), start, location.prev()));
181        } else {
182          push(ch1);
183        }         
184      }
185    } while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
186    lastLocationAWS = location.copy().prev();
187    isUnquoted = false;
188    
189    if (!more()) {
190      type = TokenType.Eof;
191    } else {
192      switch (ch) {
193      case '{' : 
194        type = TokenType.Open;
195        break;
196      case '}' : 
197        type = TokenType.Close;
198        break;
199      case '"' :
200        type = TokenType.String;
201        b.setLength(0);
202        do {
203          ch = getNextChar();
204          if (ch == '\\') {
205            ch = getNextChar();
206            switch (ch) {
207            case '"': b.append('"'); break;
208            case '\'': b.append('\''); break;
209            case '\\': b.append('\\'); break;
210            case '/': b.append('/'); break;
211            case 'n': b.append('\n'); break;
212            case 'r': b.append('\r'); break;
213            case 't': b.append('\t'); break;
214            case 'u': b.append((char) Integer.parseInt(getNext(4), 16)); break;
215            default :
216              throw error("unknown escape sequence: \\"+ch);
217            }
218            ch = ' ';
219          } else if (ch != '"')
220            b.append(ch);
221        } while (more() && (ch != '"'));
222        if (!more())
223          throw error("premature termination of json stream during a string");
224        value = b.toString();
225        break;
226      case ':' : 
227        type = TokenType.Colon;
228        break;
229      case ',' : 
230        type = TokenType.Comma;
231        break;
232      case '[' : 
233        type = TokenType.OpenArray;
234        break;
235      case ']' : 
236        type = TokenType.CloseArray;
237        break;
238      default:
239        if ((ch >= '0' && ch <= '9') || ch == '-') {
240          type = TokenType.Number;
241          b.setLength(0);
242          while (more() && ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') || ch == '+' || ch == 'e' || ch == 'E') {
243            b.append(ch);
244            ch = getNextChar();
245          }
246          value = b.toString();
247          push(ch);
248        } else if (Utilities.isAlphabetic(ch) || (ch == '_')) {
249          type = TokenType.String;
250          isUnquoted = true;
251          b.setLength(0);
252          while (more() && (Utilities.isAlphabetic(ch) || Utilities.isDigit(ch) || Utilities.existsInList(ch, '_', '.', '-'))) {
253            b.append(ch);
254            ch = getNextChar();
255          }
256          value = b.toString();
257          push(ch);
258          if ("true".equals(value) || "false".equals(value)) {
259            this.type = TokenType.Boolean;
260            isUnquoted = false;
261          } else if ("null".equals(value)) {
262            this.type = TokenType.Null;
263            isUnquoted = false;
264          } else if (!allowUnquotedStrings) {
265            throw error("Unexpected token '"+value+"' in json stream");
266          } 
267        }
268      }
269    }
270  }
271
272  public String consume(TokenType type) throws IOException {
273    if (this.type != type)
274      throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString());
275    String result = value;
276    next();
277    return result;
278  }
279
280  public JsonLocationData getLocation() {
281    return location;
282  }
283
284  public Stack<State> getStates() {
285    return states;
286  }
287
288  public void takeComments(JsonElement child) {
289    if (!comments.isEmpty()) {
290      child.getComments().addAll(comments);
291      comments.clear();
292    }
293  }
294
295  public boolean isUnquoted() {
296    return isUnquoted;
297  }
298
299  @Override
300  public String toString() {
301    return "JsonLexer [cursor=" + cursor + ", peek=" + peek + ", type=" + type + ", location=" + location.toString() + "]";
302  }
303
304  public String getSourceName() {
305    return sourceName;
306  }
307
308  public void setSourceName(String sourceName) {
309    this.sourceName = sourceName;
310  }
311
312
313}