ridencww / goldengine

Java implementation of Devin Cook's GOLD Parser engine
Other
35 stars 14 forks source link

In Linux the row (line) doesn't increment when the text has only new empty lines. #17

Closed mbrassesco closed 10 months ago

mbrassesco commented 6 years ago

When I use your engine in Linux the row (line) doesn't increment when the text has only new empty lines.

For example:

A = 2

wrdjsadfklja

Error message: Syntax error at line 2, column 1.

Should be

Syntax error at line 6, column 1.

I suspect the issue is here:

Parser.java (class)

private void consumeBuffer(int count) { if (count > 0 && count <= lookaheadBuffer.length()) { // Adjust position for (int i = 0; i < count; i++) { char c = lookaheadBuffer.charAt(i); if (c == 0x0A) { if (sysPosition.getColumn() > 1) { // Increment row if Unix EOLN (LF) sysPosition.incrementLine(); } } else if (c == 0x0D) { sysPosition.incrementLine(); } else { sysPosition.incrementColumn(); } }

codemanyak commented 6 years ago

For the Structorizer project, we had the same problem and solved it indeed by the folllowing patches:

  1. Parser.java

    /**
     * Consume/remove characters from the front of the lookahead buffer
     * and adjust the value of the system Position object.
     * @param count the number of characters to consume
     */
    private void consumeBuffer(int count) {
        if (count > 0 && count <= lookaheadBuffer.length()) {
            // Adjust position
            // START SSO 2017-06-26 - line counts were wrong
            int lines = 0, columns = 0;
            for (int i = 0; i < count; i++) {
                char c = lookaheadBuffer.charAt(i);
                switch (c) {
                case 0x0D:
                    // increment char counter for Windows LF (MacOS would be plain 0x0D)
                    if (i + 1 != count && lookaheadBuffer.charAt(i + 1) == 0x0A) {
                        i++;
                    }
                    // Fall through
                case 0x0A:
                    lines++;
                    break;
                default:
                    columns++;
                    break;
                }
            }
    
            if (lines != 0) {
                sysPosition.incrementLine(lines);
            } else {
                sysPosition.incrementColumn(columns);
            }
            // END SSO 2017-06-26
    
            // Remove the characters
            lookaheadBuffer.delete(0, count);
        }
    }
  2. Position.java

    // START SSO 2017-06-26 - Added as convenience for Parser.consumeBuffer(int) fix
    /** increment current position by given lines, reset column to 1 */
    public void incrementLine(int lines) {
        this.line += lines;
        this.column = 1;
    }
    
    /** increment current position by given columns */
    public void incrementColumn(int columns) {
        this.column += columns;
    }
    // END SSO 2017-06-26
ridencww commented 6 years ago

Thanks for bringing this to my attention and for a solution. I'll write a unit and make the necessary edits and create a release in the next few days.