Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid throwing IOExceptions #6

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bugs/bug001/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
<configuration>
<mainClass>clc.ComplexLineComment</mainClass>
<arguments>
<argument>${project.basedir}/files/clc1.in2</argument>
<argument>${project.basedir}/files/clc1.in</argument>
<argument>${project.basedir}/files/clc1.out</argument>
<argument>${project.basedir}/files/clc1.err</argument>
</arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,43 @@ public void Done()
{
buffer = null;
}

private char nextChar;

/**
* Returns next character.
*
* @return next character in the input
*/
public char getNextChar() {
return nextChar;
}

/**
* Checks next character.
*
* @return whether next character is available
*/
public boolean hasNextChar() {
try {
nextChar = readChar();
return true;
} catch (java.io.IOException ex) {
return false;
}
}

/**
* Checks next character and marks new token.
*
* @return whether next character is available
*/
public boolean hasNextToken() {
try {
nextChar = BeginToken();
return true;
} catch (java.io.IOException ex) {
return false;
}
}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
<configuration>
<projectsDirectory>bugs</projectsDirectory>
<settingsFile>bugs/settings.xml</settingsFile>
<ignoreFailures>true</ignoreFailures>
<ignoreFailures>false</ignoreFailures>
</configuration>
</execution>

Expand All @@ -142,7 +142,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
<configuration>
<projectsDirectory>examples</projectsDirectory>
<settingsFile>examples/settings.xml</settingsFile>
<ignoreFailures>true</ignoreFailures>
<ignoreFailures>false</ignoreFailures>
</configuration>
</execution>

Expand All @@ -155,7 +155,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
<configuration>
<projectsDirectory>grammars</projectsDirectory>
<settingsFile>grammars/settings.xml</settingsFile>
<ignoreFailures>true</ignoreFailures>
<ignoreFailures>false</ignoreFailures>
</configuration>
</execution>

Expand Down
19 changes: 17 additions & 2 deletions src/main/resources/templates/CharStream.template
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@ interface CharStream {
/**
* Returns the next character from the selected input. The method
* of selecting the input is the responsibility of the class
* implementing this interface. Can throw any java.io.IOException.
* implementing this interface. Can throw IndexOutOfBoundsException
* if there are no more characters.
*/
char readChar() throws java.io.IOException;
char getNextChar();

/**
* Checks whether there are unread characters in the input.
*
* @return whether next character exists
*/
boolean hasNextChar();

/**
* Similar to hasNextCharacter, but may have additional side effects.
*
* @return whether next character exists
*/
boolean hasNextToken();

/**
* Returns the column position of the character last read.
Expand Down
179 changes: 105 additions & 74 deletions src/main/resources/templates/JavaCharStream.template
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class JavaCharStream {
case 'f':
case 'F':
return 15;
default:
throw new java.io.IOException(); // Should never come here
default: // Should never come here
throw new IllegalArgumentException("Invalid hax character :" + c);
}
}

Expand Down Expand Up @@ -142,7 +142,7 @@ class JavaCharStream {
tokenBegin = 0;
}

${PREFIX}protected void FillBuff() throws java.io.IOException {
${PREFIX}protected boolean FillBuff() {
int i;
if (maxNextCharInd == 4096) {
maxNextCharInd = nextCharInd = 0;
Expand All @@ -151,50 +151,40 @@ class JavaCharStream {
if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
4096 - maxNextCharInd)) == -1) {
inputStream.close();
throw new java.io.IOException();
reset();
return false;
} else {
maxNextCharInd += i;
return true;
}
} catch (java.io.IOException e) {
if (bufpos != 0) {
--bufpos;
backup(0);
#if KEEP_LINE_COLUMN
} else {
bufline[bufpos] = line;
bufcolumn[bufpos] = column;
#fi
}
throw e;
reset();
return false;
}
}

${PREFIX}protected char ReadByte() throws java.io.IOException {
if (++nextCharInd >= maxNextCharInd) {
FillBuff();
private void reset() {
if (bufpos != 0) {
--bufpos;
backup(0);
#if KEEP_LINE_COLUMN
} else {
bufline[bufpos] = line;
bufcolumn[bufpos] = column;
#fi
}
return nextCharBuf[nextCharInd];
}

/**
* Gets starting character for token.
*
* @return starting character for token. */
${PREFIX}public char BeginToken() throws java.io.IOException {
if (inBuf > 0) {
--inBuf;
private char nextByte;

if (++bufpos == bufsize) {
bufpos = 0;
${PREFIX}protected boolean hasNextByte() {
if (++nextCharInd >= maxNextCharInd) {
if (!FillBuff()) {
return false;
}
tokenBegin = bufpos;
return buffer[bufpos];
}

tokenBegin = 0;
bufpos = -1;

return readChar();
nextByte = nextCharBuf[nextCharInd];
return true;
}

${PREFIX}protected void AdjustBuffSize() {
Expand Down Expand Up @@ -250,23 +240,60 @@ class JavaCharStream {
}
#fi

private char nextChar;

/**
* Returns next character.
*
* @return next character in the input
*/
${PREFIX}public char getNextChar() {
return nextChar;
}

/**
* Checks next character and marks new token.
*
* @return whether next character is available
*/
${PREFIX}public boolean hasNextToken() {
if (inBuf > 0) {
--inBuf;

if (++bufpos == bufsize) {
bufpos = 0;
}
tokenBegin = bufpos;
nextChar = buffer[bufpos];
return true;
}

tokenBegin = 0;
bufpos = -1;
return hasNextChar();
}

/** Read a character. */
${PREFIX}public char readChar() throws java.io.IOException {
${PREFIX}public boolean hasNextChar() {
if (inBuf > 0) {
--inBuf;

if (++bufpos == bufsize) {
bufpos = 0;
}
return buffer[bufpos];
nextChar = buffer[bufpos];
return true;
}

char c;

if (++bufpos == available) {
AdjustBuffSize();
}
if ((buffer[bufpos] = c = ReadByte()) == '\\') {
if (!hasNextByte()) {
return false;
}
if ((buffer[bufpos] = c = nextByte) == '\\') {
#if KEEP_LINE_COLUMN
UpdateLineColumn(c);
#fi
Expand All @@ -277,29 +304,31 @@ class JavaCharStream {
if (++bufpos == available) {
AdjustBuffSize();
}
try {
if ((buffer[bufpos] = c = ReadByte()) != '\\') {
#if KEEP_LINE_COLUMN
UpdateLineColumn(c);
#fi
// found a non-backslash char.
if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
if (--bufpos < 0) {
bufpos = bufsize - 1;
}
break;
}

backup(backSlashCnt);
return '\\';
}
} catch (java.io.IOException e) {
if (!hasNextByte()) {
// We are returning one backslash so we should only backup (count-1)
if (backSlashCnt > 1) {
backup(backSlashCnt - 1);
}

return '\\';
nextChar = '\\';
return true;
}
if ((buffer[bufpos] = c = nextByte) != '\\') {
#if KEEP_LINE_COLUMN
UpdateLineColumn(c);
#fi
// found a non-backslash char.
if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
if (--bufpos < 0) {
bufpos = bufsize - 1;
}
break;
}

backup(backSlashCnt);
nextChar = '\\';
return true;
}

#if KEEP_LINE_COLUMN
Expand All @@ -309,42 +338,44 @@ class JavaCharStream {
}

// Here, we have seen an odd number of backslash's followed by a 'u'
try {
while ((c = ReadByte()) == 'u') {
while (hasNextByte() && nextByte == 'u') {
#if KEEP_LINE_COLUMN
++column;
++column;
#else
;
;
#fi
}
buffer[bufpos] = c = (char) (hexval(c) << 12
| hexval(ReadByte()) << 8
| hexval(ReadByte()) << 4
| hexval(ReadByte()));

#if KEEP_LINE_COLUMN
column += 4;
#fi
} catch (java.io.IOException e) {
}
c = (char) (hexval(nextByte) << 12);
for (int shift = 8; shift >= 0; shift -= 4) {
if (!hasNextByte()) {
#if KEEP_LINE_COLUMN
throw new ${LEGACY_EXCEPTION_HANDLING?Error:RuntimeException}("Invalid escape character at line " + line
throw new ${LEGACY_EXCEPTION_HANDLING?Error:RuntimeException}("Invalid escape character at line " + line
+ " column " + column + ".");
#else
throw new ${LEGACY_EXCEPTION_HANDLING?Error:RuntimeException}(\"Invalid escape character in input\");
throw new ${LEGACY_EXCEPTION_HANDLING?Error:RuntimeException}(\"Invalid escape character in input\");
#fi
}

}
c |= hexval(nextByte) << shift;
}
buffer[bufpos] = c;
#if KEEP_LINE_COLUMN
column += 4;
#fi
if (backSlashCnt == 1) {
return c;
nextChar = c;
return true;
} else {
backup(backSlashCnt - 1);
return '\\';
nextChar = '\\';
return true;
}
} else {
#if KEEP_LINE_COLUMN
UpdateLineColumn(c);
#fi
return c;
nextChar = c;
return true;
}
}

Expand Down
Loading