public Object invoke(Object reader, Object pct, Object opts, Object pendingForms) { PushbackReader r = (PushbackReader) reader; if(ARG_ENV.deref() == null) { return interpretToken(readToken(r, '%')); } int ch = read1(r); unread(r, ch); //% alone is first arg if(ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch)) { return registerArg(1); } Object n = read(r, true, null, true, opts, ensurePending(pendingForms)); if(n.equals(Compiler._AMP_)) return registerArg(-1); if(!(n instanceof Number)) throw new IllegalStateException("arg literal must be %, %& or %integer"); return registerArg(((Number) n).intValue()); }
static private String readToken(PushbackReader r, char initch, boolean leadConstituent) { StringBuilder sb = new StringBuilder(); if(leadConstituent && nonConstituent(initch)) throw Util.runtimeException("Invalid leading character: " + (char)initch); sb.append(initch); for(; ;) { int ch = read1(r); if(ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch)) { unread(r, ch); return sb.toString(); } else if(nonConstituent(ch)) throw Util.runtimeException("Invalid constituent character: " + (char)ch); sb.append((char) ch); } }
/** Parser that eats everything until two consecutive dashes (inclusive) */ private void parseComment( PushbackReader in ) throws IOException, WrongDTDException { int state = COMM_TEXT; for( ;; ) { int i = in.read(); if( i == -1 ) break; // EOF switch( state ) { case COMM_TEXT: if( i == '-' ) state = COMM_DASH; break; case COMM_DASH: if( i == '-' ) return; // finished eating comment state = COMM_TEXT; break; } } throw new WrongDTDException( "Premature end of DTD" ); // NOI18N }
/** Parser that reads the name of entity reference and replace it with * the content of that entity (using the pushback capability of input). * It gets the control just after starting '%' * @returns the name of reference which was replaced. */ private String parseEntityReference( PushbackReader in ) throws IOException, WrongDTDException { StringBuffer sb = new StringBuffer(); for( ;; ) { int i = in.read(); if( i == -1 ) break; // EOF if( isNameChar( (char)i ) ) { sb.append( (char)i ); // next char of name } else { String entValue = (String)entityMap.get( sb.toString() ); //get the entity content if( entValue == null ) throw new WrongDTDException( "No such entity: \"" + sb + "\"" ); // NOI18N if( i != ';' ) in.unread( i ); in.unread( entValue.toCharArray() ); // push it back to stream return sb.toString(); } } throw new WrongDTDException( "Premature end of DTD" ); // NOI18N }
private static Object readToken(PushbackReader r, IEOFHandler ieofHandler, int ch) throws IOException { StringBuilder sb = new StringBuilder(); sb.append((char)ch); while(true) { ch = r.read(); if (ch == -1) { ieofHandler.eof(); break; } else if (Character.isWhitespace(ch)) { break; } else if (ch == ')') { r.unread(ch); break; } else { sb.append((char)ch); } } return interpretToken(sb.toString()); }
/** * Extracts matching character from stream. * A non-matching character is pushed back to the stream. * * @param in The input. * @param c The character to expect from the stream. * * @return <code>true</code> if the specified character was extracted from the stream, <code>false</code> otherwise. * * @exception java.io.IOException In case of read error in the stream. */ public static boolean readMatch(PushbackReader in, int c) throws IOException { int i = in.read(); if (i != c) { if (i != -1) { in.unread(i); } return false; } return true; }
/** * Extracts whitespace from stream. * * @param in The input. * * @exception java.io.IOException In case of read error in the stream. */ public static void extractWhitespace(PushbackReader in) throws IOException { int c; while (Character.isWhitespace((char) (c = in.read()))) { // Extracts any whitespace } if (c != -1) { in.unread(c); } }
/** * Reads an aprational from a reader. The specified radix is used. * * @param in The input stream. * @param radix The radix to be used. * * @exception java.io.IOException In case of I/O error reading the stream. * @exception java.lang.NumberFormatException In case the number is invalid. * @exception java.lang.IllegalArgumentException In case the denominator is zero. * * @see #Aprational(PushbackReader) */ public Aprational(PushbackReader in, int radix) throws IOException, NumberFormatException, IllegalArgumentException, ApfloatRuntimeException { this.numerator = new Apint(in, radix); ApfloatHelper.extractWhitespace(in); if (!ApfloatHelper.readMatch(in, '/')) { this.denominator = ONES[radix]; return; } ApfloatHelper.extractWhitespace(in); this.denominator = new Apint(in, radix); checkDenominator(); reduce(); }
private static long readExponent(PushbackReader in) throws IOException, NumberFormatException { StringBuilder buffer = new StringBuilder(20); int input; for (long i = 0; (input = in.read()) != -1; i++) { char c = (char) input; int digit = Character.digit(c, 10); // Exponent is always in base 10 if (i == 0 && c == '-' || digit != -1) { buffer.append(c); } else { // Stop at first invalid character and put it back in.unread(input); break; } } return Long.parseLong(buffer.toString()); }
/** * Initializes the XML reader. * * @param reader the input for the XML data. */ public StdXMLReader(Reader reader) { this.currentReader = new StackedReader(); this.readers = new Stack(); this.currentReader.lineReader = new LineNumberReader(reader); this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2); this.currentReader.publicId = ""; try { this.currentReader.systemId = new URL("file:."); } catch (MalformedURLException e) { // never happens } }
/** * Initializes the XML reader. * * @param stream the input for the XML data. * * @throws java.io.IOException * if an I/O error occurred */ public StdXMLReader(InputStream stream) throws IOException { PushbackInputStream pbstream = new PushbackInputStream(stream); StringBuffer charsRead = new StringBuffer(); Reader reader = this.stream2reader(stream, charsRead); this.currentReader = new StackedReader(); this.readers = new Stack(); this.currentReader.lineReader = new LineNumberReader(reader); this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2); this.currentReader.publicId = ""; try { this.currentReader.systemId = new URL("file:."); } catch (MalformedURLException e) { // never happens } this.startNewStream(new StringReader(charsRead.toString())); }
/** * Starts a new stream from a Java reader. The new stream is used * temporary to read data from. If that stream is exhausted, control * returns to the parent stream. * * @param reader the non-null reader to read the new data from * @param isInternalEntity true if the reader is produced by resolving * an internal entity */ public void startNewStream(Reader reader, boolean isInternalEntity) { StackedReader oldReader = this.currentReader; this.readers.push(this.currentReader); this.currentReader = new StackedReader(); if (isInternalEntity) { this.currentReader.lineReader = null; this.currentReader.pbReader = new PushbackReader(reader, 2); } else { this.currentReader.lineReader = new LineNumberReader(reader); this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2); } this.currentReader.systemId = oldReader.systemId; this.currentReader.publicId = oldReader.publicId; }
public static void main(String[] args) { for (String filename : args) { File file = new File(filename); Translation translation = new Translation(filename + ".parse"); try { Parser p = new Parser( new Lexer( new PushbackReader(new FileReader(file), 1024) ) ); Start tree = p.parse(); tree.apply(translation); System.out.println(filename + " parsed successfully."); } catch (ParserException | LexerException | IOException e) { System.err.println("Error parsing " + filename + ": " + e.getMessage()); translation.die(e.getMessage()); } } }
public Object invoke(Object reader, Object firstChar, Object opts, Object pendingForms){ PushbackReader r = (PushbackReader) reader; pendingForms = ensurePending(pendingForms); Object name = read(r, true, null, false, opts, pendingForms); if (!(name instanceof Symbol)) throw new RuntimeException("Reader tag must be a symbol"); Symbol sym = (Symbol)name; Object form = read(r, true, null, true, opts, pendingForms); if(isPreserveReadCond(opts) || RT.suppressRead()) { return TaggedLiteral.create(sym, form); } else { return sym.getName().contains(".") ? readRecord(form, sym, opts, pendingForms) : readTagged(form, sym, opts, pendingForms); } }
static private Object readNumber(PushbackReader r, char initch) { StringBuilder sb = new StringBuilder(); sb.append(initch); for(; ;) { int ch = read1(r); if(ch == -1 || isWhitespace(ch) || isMacro(ch)) { unread(r, ch); break; } sb.append((char) ch); } String s = sb.toString(); Object n = matchNumber(s); if(n == null) throw new NumberFormatException("Invalid number: " + s); return n; }
static private int readUnicodeChar(PushbackReader r, int initch, int base, int length, boolean exact) { int uc = Character.digit(initch, base); if(uc == -1) throw new IllegalArgumentException("Invalid digit: " + (char) initch); int i = 1; for(; i < length; ++i) { int ch = read1(r); if(ch == -1 || isWhitespace(ch) || isMacro(ch)) { unread(r, ch); break; } int d = Character.digit(ch, base); if(d == -1) throw new IllegalArgumentException("Invalid digit: " + (char) ch); uc = uc * base + d; } if(i != length && exact) throw new IllegalArgumentException("Invalid character length: " + i + ", should be: " + length); return uc; }
private Object readTagged(PushbackReader reader, Symbol tag, IPersistentMap opts){ Object o = read(reader, true, null, true, opts); ILookup readers = (ILookup)RT.get(opts, READERS); IFn dataReader = (IFn)RT.get(readers, tag); if(dataReader == null) dataReader = (IFn)RT.get(RT.DEFAULT_DATA_READERS.deref(),tag); if(dataReader == null){ IFn defaultReader = (IFn)RT.get(opts, DEFAULT); if(defaultReader != null) return defaultReader.invoke(tag, o); else throw new RuntimeException("No reader function for tag " + tag.toString()); } else return dataReader.invoke(o); }
public Object invoke(Object reader, Object hash, Object opts) { int ch = read1((Reader) reader); if(ch == -1) throw Util.runtimeException("EOF while reading character"); IFn fn = dispatchMacros[ch]; if(fn == null) { //try tagged reader if(Character.isLetter(ch)) { unread((PushbackReader) reader, ch); return taggedReader.invoke(reader, ch, opts); } throw Util.runtimeException(String.format("No dispatch macro for: %c", (char) ch)); } return fn.invoke(reader, ch, opts); }
public Object invoke(Object reader, Object leftparen, Object opts) { PushbackReader r = (PushbackReader) reader; int line = -1; int column = -1; if(r instanceof LineNumberingPushbackReader) { line = ((LineNumberingPushbackReader) r).getLineNumber(); column = ((LineNumberingPushbackReader) r).getColumnNumber()-1; } List list = readDelimitedList(')', r, true, opts); if(list.isEmpty()) return PersistentList.EMPTY; IObj s = (IObj) PersistentList.create(list); // IObj s = (IObj) RT.seq(list); // if(line != -1) // { // return s.withMeta(RT.map(RT.LINE_KEY, line, RT.COLUMN_KEY, column)); // } // else return s; }
public static List readDelimitedList(char delim, PushbackReader r, boolean isRecursive, Object opts, Object pendingForms) { final int firstline = (r instanceof LineNumberingPushbackReader) ? ((LineNumberingPushbackReader) r).getLineNumber() : -1; ArrayList a = new ArrayList(); for(; ;) { Object form = read(r, false, READ_EOF, delim, READ_FINISHED, isRecursive, opts, pendingForms); if (form == READ_EOF) { if (firstline < 0) throw Util.runtimeException("EOF while reading"); else throw Util.runtimeException("EOF while reading, starting at line " + firstline); } else if (form == READ_FINISHED) { return a; } a.add(form); } }
public Object invoke(Object reader, Object leftparen, Object opts, Object pendingForms) { PushbackReader r = (PushbackReader) reader; int line = -1; int column = -1; if(r instanceof LineNumberingPushbackReader) { line = ((LineNumberingPushbackReader) r).getLineNumber(); column = ((LineNumberingPushbackReader) r).getColumnNumber()-1; } List list = readDelimitedList(')', r, true, opts, ensurePending(pendingForms)); if(list.isEmpty()) return PersistentList.EMPTY; IObj s = (IObj) PersistentList.create(list); // IObj s = (IObj) RT.seq(list); if(line != -1) { return s.withMeta(RT.map(RT.LINE_KEY, line, RT.COLUMN_KEY, column)); } else return s; }
/** Parser that reads the markup type after <!. Recognizes ENTITY, ELEMENT * and ATTLIST markup and forwards their processing to proper parser. * It gets the control just after starting "<!" and releases after eating * final '>' */ private void parseMarkup( PushbackReader in ) throws IOException, WrongDTDException { StringBuffer sb = new StringBuffer(); for( ;; ) { int i = in.read(); if( i == -1 ) throw new WrongDTDException( "Premature end of DTD" ); // NOI18N EOF if( i == ' ' ) break; sb.append( (char)i ); // next char of name } String markup = sb.toString(); switch (markup) { case "ENTITY": // NOI18N parseEntityDefinition( in ); break; case "ELEMENT": // NOI18N parseElement( in ); break; case "ATTLIST": // NOI18N parseAttlist( in ); break; default: throw new WrongDTDException( "Wrong DTD markup <!" + markup ); } }
/** Reads a line and returns the char sequence for newline */ private static String readLine(PushbackReader r, StringBuffer nl) throws IOException { StringBuffer line = new StringBuffer(); int ic = r.read(); if (ic == -1) return null; char c = (char) ic; while (c != '\n' && c != '\r') { line.append(c); ic = r.read(); if (ic == -1) break; c = (char) ic; } if (nl != null) { nl.append(c); } if (c == '\r') { try { ic = r.read(); if (ic != -1) { c = (char) ic; if (c != '\n') r.unread(c); else if (nl != null) nl.append(c); } } catch (IOException ioex) {} } return line.toString(); }
private boolean compareText(PushbackReader source, String text) throws IOException { if (text == null || text.length() == 0) return true; text = adjustTextNL(text); char[] chars = new char[text.length()]; int pos = 0; int n; String readStr = ""; do { n = source.read(chars, 0, chars.length - pos); if (n > 0) { pos += n; readStr = readStr + new String(chars, 0, n); } if (readStr.endsWith("\r")) { try { char c = (char) source.read(); if (c != '\n') source.unread(c); else readStr += c; } catch (IOException ioex) {} } readStr = adjustTextNL(readStr); pos = readStr.length(); } while (n > 0 && pos < chars.length); readStr.getChars(0, readStr.length(), chars, 0); line += numChars('\n', chars); //System.out.println("Comparing text of the diff:\n'"+text+"'\nWith the read text:\n'"+readStr+"'\n"); //System.out.println(" EQUALS = "+readStr.equals(text)); return readStr.equals(text); }
public AbstractObjectVariable (JPDADebuggerImpl debugger, Value value, String genericSignature, String id) { this(debugger, value, id); try { if (genericSignature != null) { this.genericType = getTypeDescription(new PushbackReader(new StringReader(genericSignature), 1)); } } catch (IOException e) { /// invalid signature } }
public static List<Object> readAll(PushbackReader r) throws IOException { ArrayList<Object> l = new ArrayList<Object>(); Object res; while((res = read(r)) != null) { l.add(res); } return l; }
public static Object read(PushbackReader r, IEOFHandler eofHandler) throws IOException { int ch; do { ch = r.read(); } while (Character.isWhitespace(ch)); switch(ch) { case -1: return eofHandler.eof(); case ')': throw new RuntimeException("unmatched ')'"); case '(': return readList(r); case '"': return readString(r); default: return readToken(r, eofHandler, ch); } }
private static List readList(PushbackReader r) throws IOException { ArrayList list = new ArrayList(); while(true) { int ch = r.read(); if (ch == -1) throw new EOFException(); if (ch == ')') break; r.unread(ch); list.add(read(r, ThrowOnEof)); } return list; }
/** * Construct a Tokenizer from an abstract Reader object * @param reader reader object */ public Tokenizer(Reader reader) { this.reader = new PushbackReader(reader); this.nextToken = fetch(); this.lineNumber = 1; }
/** * C'tor * @param in Cannot be null */ public THJSONInputStream(Reader in) { if (in instanceof PushbackReader) { this.in = (PushbackReader) in; } else { this.in = new PushbackReader(requireNonNull(in, "in cannot be null")); } }