Skip to content

Commit

Permalink
https://github.com/davemckain/snuggletex/issues/4
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Jul 21, 2023
1 parent a6352bd commit 3b6df98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,14 @@ public void test403() {
assertEquals(ast.toString(), "y*Sin(x)");
}

public void test404() {
// issue #794
String tex = "x^.5";
TeXParser teXSliceParser = new TeXParser();
IExpr ast = teXSliceParser.parse(tex);
assertEquals(ast.toString(), "Sqrt(x)");
}

public void check(String strEval, String strResult) {
IExpr expr = texConverter.parse(strEval);
assertEquals(expr.toString(), strResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ public ModeState(final TokenisationMode tokenisationMode, final LaTeXMode latexM
this.foundTerminator = false;
}

/**
* Gets the current last token recorded in this mode.
*/
public FlowToken getLastToken() {
return !tokens.isEmpty() ? tokens.get(tokens.size() - 1) : null;
}

/**
* Gets the end index of the {@link FrozenSlice} corresponding to the last {@link Token}
* recorded, returning the start position if nothing has been recorded. This is useful for
Expand Down Expand Up @@ -548,11 +555,16 @@ private FlowToken readNextTokenMathMode() throws SnuggleParseException {
return createError(CoreErrorCode.TTEM04, position, position + 1);

default:
/* Mathematical symbol, operator, number etc... */
List<FlowToken> list = currentModeState.tokens;
if (list.size() > 0) {
CharSequence extract = list.get(list.size() - 1).getSlice().extract();
/*
* Mathematical symbol, operator, number etc... We need to check whether we're following up
* a sub/superscript and, if so, read only a single character.
*/
FlowToken lastToken = currentModeState.getLastToken();
if (lastToken != null
&& lastToken.hasInterpretationType(InterpretationType.MATH_OPERATOR)) {
CharSequence extract = lastToken.getSlice().extract();
if ("_".equals(extract) || "^".equals(extract)) {
/* This is following sub/superscript so only read single character */
return readNextMathNumberOrSymbol(true);
}
}
Expand Down Expand Up @@ -607,16 +619,20 @@ private SimpleToken tryReadMathNumber(boolean singleDigitMode) {
boolean foundDigitsBeforeDecimalPoint = false;
boolean foundDigitsAfterDecimalPoint = false;
boolean foundDecimalPoint = false;

if (singleDigitMode) {
c = workingDocument.charAt(index);
if (c >= '0' && c <= '9') {
FrozenSlice digitSlice = workingDocument.freezeSlice(position, position + 1);
return new SimpleToken(digitSlice, TokenType.MATH_NUMBER, LaTeXMode.MATH, null,
new MathNumberInterpretation(digitSlice.extract()));
}
}
/* Read zero or more digits */
while (true) {
c = workingDocument.charAt(index);
if (c >= '0' && c <= '9') {
foundDigitsBeforeDecimalPoint = true;
index++;
if (singleDigitMode) {
break;
}
} else {
break;
}
Expand All @@ -631,22 +647,20 @@ private SimpleToken tryReadMathNumber(boolean singleDigitMode) {
if (!foundDigitsBeforeDecimalPoint && !foundDecimalPoint) {
return null;
}
if (!singleDigitMode) {
/* Read zero or more digits */
while (true) {
c = workingDocument.charAt(index);
if (c >= '0' && c <= '9') {
foundDigitsAfterDecimalPoint = true;
index++;
} else {
break;
}
}
/* Make sure we read in some number! */
if (!foundDigitsBeforeDecimalPoint && !foundDigitsAfterDecimalPoint) {
return null;
/* Read zero or more digits */
while (true) {
c = workingDocument.charAt(index);
if (c >= '0' && c <= '9') {
foundDigitsAfterDecimalPoint = true;
index++;
} else {
break;
}
}
/* Make sure we read in some number! */
if (!foundDigitsBeforeDecimalPoint && !foundDigitsAfterDecimalPoint) {
return null;
}
FrozenSlice numberSlice = workingDocument.freezeSlice(position, index);
return new SimpleToken(numberSlice, TokenType.MATH_NUMBER, LaTeXMode.MATH, null,
new MathNumberInterpretation(numberSlice.extract()));
Expand Down

0 comments on commit 3b6df98

Please sign in to comment.