Skip to content

Commit

Permalink
Handle nested block comments and plus signs in coordinates (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler committed Jun 20, 2024
1 parent cdd215d commit 2cf1092
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.ugs.nbp.editor.lexer;

import static org.apache.commons.lang3.CharUtils.isAsciiNumeric;
import org.netbeans.api.lexer.Token;
import org.netbeans.spi.lexer.Lexer;
import org.netbeans.spi.lexer.LexerInput;
import org.netbeans.spi.lexer.LexerRestartInfo;

import static org.apache.commons.lang3.CharUtils.isAsciiNumeric;

/**
* A basic lexer for parsing a gcode file to tokens for describing the gcode elements.
*
Expand Down Expand Up @@ -128,13 +127,18 @@ private Token<GcodeTokenId> parseWhitespace(int character) {
}

private Token<GcodeTokenId> parseCommentSection() {
int nestedBlocks = 0;
int ch;
while (true) {
ch = input.read();

if (ch == LexerInput.EOF || ch == '\r' || ch == '\n') {
input.backup(1);
return createToken(GcodeTokenId.ERROR);
} else if (ch == '(') {
nestedBlocks++;
} else if (ch == ')' && nestedBlocks > 0) {
nestedBlocks--;
} else if (ch == ')') {
break;
}
Expand All @@ -160,6 +164,7 @@ private Token<GcodeTokenId> parseNumericField(GcodeTokenId tokenId) {
int minusCount = 0;
int commaCount = 0;
int numberCount = 0;
int plusCount = 0;

while (true) {
char character = (char) input.read();
Expand All @@ -172,6 +177,8 @@ private Token<GcodeTokenId> parseNumericField(GcodeTokenId tokenId) {
commaCount++;
} else if (character == '-') {
minusCount++;
} else if (character == '+') {
plusCount++;
}

if (isNumeric(character)) {
Expand All @@ -181,7 +188,7 @@ private Token<GcodeTokenId> parseNumericField(GcodeTokenId tokenId) {
length++;
}

if (length == 0 || minusCount > 1 || commaCount > 1 || numberCount == 0) {
if (length == 0 || minusCount > 1 || commaCount > 1 || plusCount > 1 || numberCount == 0) {
return createToken(GcodeTokenId.ERROR);
}

Expand All @@ -195,7 +202,7 @@ private Token<GcodeTokenId> parseNumericField(GcodeTokenId tokenId) {
* @return true if the character is a part of a numeric field.
*/
private boolean isNumeric(char character) {
return isAsciiNumeric(character) || character == '-' || character == '.' || character == ',';
return isAsciiNumeric(character) || character == '-' || character == '.' || character == ',' || character == '+';
}

private Token<GcodeTokenId> parseCommand(GcodeTokenId tokenId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ public void parsingGcodeShouldIdentifyParameters() {
assertEquals("S100", t.text());
}

@Test
public void parsingGcodeShouldIdentifyParametersWithPlusSign() {
String text = "G01 X+100";
TokenSequence<GcodeTokenId> ts = parseTokenSequence(text);

ts.moveNext();
Token<?> t = ts.token();
assertEquals(GcodeTokenId.MOVEMENT, t.id());
assertEquals("G01", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.WHITESPACE, t.id());
assertEquals(" ", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.AXIS, t.id());
assertEquals("X+100", t.text());
}

@Test
public void parsingGcodeShouldIdentifyParametersWithDecimals() {
String text = "G01 X-100.1 S100.10";
Expand Down Expand Up @@ -428,4 +449,46 @@ public void parsingParametersWithSpaceShouldGenerateErrors() {
assertEquals(GcodeTokenId.ERROR, t.id());
assertEquals("3", t.text());
}

@Test
public void parsingNestedBlockComments() {
String text = "G01 (nested (comment))";
TokenSequence<GcodeTokenId> ts = parseTokenSequence(text);

ts.moveNext();
Token<?> t = ts.token();
assertEquals(GcodeTokenId.MOVEMENT, t.id());
assertEquals("G01", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.WHITESPACE, t.id());
assertEquals(" ", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.COMMENT, t.id());
assertEquals("(nested (comment))", t.text());
}

@Test
public void parsingNestedBlockCommentsWithMissingLastParanthesis() {
String text = "G01 (nested (comment)";
TokenSequence<GcodeTokenId> ts = parseTokenSequence(text);

ts.moveNext();
Token<?> t = ts.token();
assertEquals(GcodeTokenId.MOVEMENT, t.id());
assertEquals("G01", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.WHITESPACE, t.id());
assertEquals(" ", t.text());

ts.moveNext();
t = ts.token();
assertEquals(GcodeTokenId.ERROR, t.id());
assertEquals("(nested (comment)", t.text());
}
}

0 comments on commit 2cf1092

Please sign in to comment.