} /* end Consume Char */ /* --------------------------------------------------------------------------- * method NextChar() * --------------------------------------------------------------------------- * Reads the lookahead character from infile without advancing the current * reading position and returns its character code. Returns ASCII.EOT if * the lookahead character lies beyond the end of infile. * * pre-conditions: * o infile must be open * * post-conditions: * o character code of lookahead character or ASCII.EOT is returned * o current reading position and line and column counters are NOT updated * o file status is set to Success * * error-conditions: * o none * ------------------------------------------------------------------------ */ public char NextChar() { char ch; if (!isOpen) { return(ASCII.EOT); } /* end if */ if (index == buflen) { status = InfileStatus.AttemptToReadPastEOF; return(ASCII.EOT); } /* end if */ ch = (char)buffer[index]; /* return LF for CR */ if (ch == ASCII.CR) { ch = ASCII.LF; } /* end if */ status = InfileStatus.Success; return(ch); } /* end NextChar */
} /* end MarkLexeme */ /* --------------------------------------------------------------------------- * method ReadMarkedLexeme() * --------------------------------------------------------------------------- * Returns a string object with the character sequence starting with the * character that has been marked using method markLexeme() and ending * with the last consumed character. Returns null if no marker has * been set or if the marked character has not been consumed yet. * * pre-conditions: * o infile must be open * o lexeme must have been marked using method markLexeme() * o character at the marked position must have been consumed * * post-conditions: * o marked position is cleared * o string with lexeme is returned * * error-conditions: * o if no marker has been set or marked character has not been consumed, * no operation is carried out and null is returned * ------------------------------------------------------------------------ */ public string ReadMarkedLexeme() { string lexeme; ulong length; if (!isOpen) { return(null); } /* end if */ /* check pre-conditions */ if ((markerIsSet == false) || (markedIndex >= index)) { status = InfileStatus.AllocationFailed; return(null); } /* end if */ /* determine length */ length = index - markedIndex; /* copy lexeme */ lexeme = getStringForSlice(buffer, markedIndex, length); if (lexeme == null) { status = InfileStatus.AllocationFailed; return(null); } /* end if */ /* clear marker */ markerIsSet = false; return(lexeme); } /* end ReadMarkedLexeme */
} /* end Infile */ /* --------------------------------------------------------------------------- * method ReadChar() * --------------------------------------------------------------------------- * Reads the lookahead character from infile, advancing the current reading * position, updating line and column counter and returns its character code. * Returns ASCII.EOT if the lookahead character lies beyond the end of infile. * * pre-conditions: * o infile must be open * * post-conditions: * o character code of lookahead character or ASCII.EOT is returned * o current reading position and line and column counters are updated * o infile status is set to Success * * error-conditions: * o none * ------------------------------------------------------------------------ */ public char ReadChar() { char ch; if (!isOpen) { return(ASCII.EOT); } /* end if */ /* if new line encountered, update line and column counters */ if (index == buflen) { status = InfileStatus.AttemptToReadPastEOF; return(ASCII.EOT); } /* end if */ ch = (char)buffer[index]; index++; if (ch == ASCII.LF) { line++; column = 1; } else if (ch == ASCII.CR) { line++; column = 1; /* if LF follows, skip it */ if ((index < buflen) && ((char)buffer[index] == ASCII.LF)) { index++; } /* end if */ ch = ASCII.LF; } else { column++; } /* end if */ status = InfileStatus.Success; return(ch); } /* end ReadChar */
} /* end NextChar */ /* --------------------------------------------------------------------------- * method LA2Char() * --------------------------------------------------------------------------- * Reads the second lookahead character from infile without advancing the * current reading position and returns its character code. Returns ASCII.EOT * if the second lookahead character lies beyond the end of infile. * * pre-conditions: * o infile must be open * * post-conditions: * o character code of second lookahead character or EOT is returned * o current reading position and line and column counters are NOT updated * o file status is set to Success * * error-conditions: * o none * ------------------------------------------------------------------------ */ public char LA2Char() { char la2; if (!isOpen) { return(ASCII.EOT); } /* end if */ if (index + 1 == buflen) { status = InfileStatus.AttemptToReadPastEOF; return(ASCII.EOT); } /* end if */ la2 = (char)buffer[index + 1]; /* skip CR LF sequence if encountered */ if ((buffer[index] == ASCII.CR) && (la2 == ASCII.LF)) { if (index + 2 == buflen) { status = InfileStatus.AttemptToReadPastEOF; return(ASCII.EOT); } /* end if */ la2 = (char)buffer[index + 2]; } /* end if */ /* return LF for CR */ if (la2 == ASCII.CR) { la2 = ASCII.LF; } /* end if */ status = InfileStatus.Success; return(la2); } /* end LA2Char */
/* --------------------------------------------------------------------------- * factory method Open(filename) * --------------------------------------------------------------------------- * Opens the given file, creates a new infile instance, associates the file * with the newly created instance and returns a result pair with the infile * reference and a status code. * * pre-conditions: * o filename must reference an existing, accessible file. * * post-conditions: * o new infile created and returned * o line and column counters of the newly created infile are set to 1 * o Success is returned in status * * error-conditions: * o if the file represented by filename cannot be found * infile is null, status is FileNotFound * o if the file represented by filename cannot be accessed * infile is null, status is FileAccessDenied * ------------------------------------------------------------------------ */ public static Result <IInfile, InfileStatus> Open(string filename) { InfileStatus status; Infile infile = null; byte[] buffer = null; ulong buflen = 0; ulong bytesRead = 0; if (string.IsNullOrEmpty(filename)) { status = InfileStatus.InvalidReference; return(new Result <IInfile, InfileStatus>(null, status)); } /* end if */ var result = FileIO.Open(filename, FileIOMode.Read); if (result.Value() == null) { switch (result.Status()) { case FileIOStatus.InvalidReference: status = InfileStatus.InvalidReference; break; case FileIOStatus.FileNotFound: status = InfileStatus.FileNotFound; break; case FileIOStatus.FileAccessDenied: status = InfileStatus.FileAccessDenied; break; default: status = InfileStatus.IOSubsystemError; break; } /* end switch */ return(new Result <IInfile, InfileStatus> (null, status)); } /* end if */ result.Value().ReadNBytes(ref buffer, buflen, out bytesRead); result.Value().Close(); if ((buffer == null) || (buflen == 0)) { status = InfileStatus.SourceFileIsEmpty; return(new Result <IInfile, InfileStatus> (null, status)); } /* end if */ infile = new Infile(); infile.filename = filename; infile.index = 0; infile.line = 1; infile.column = 1; infile.markerIsSet = false; infile.markedIndex = 0; infile.buflen = buflen; infile.buffer = buffer; infile.status = InfileStatus.Success; infile.isOpen = true; return(new Result <IInfile, InfileStatus>(infile, infile.status)); } /* end Open */