Skip to content

Commit

Permalink
added segments to the input to contain the built-in macro nesting pat…
Browse files Browse the repository at this point in the history
…h in errors
  • Loading branch information
verhas committed Dec 11, 2024
1 parent a8522d0 commit ec37ee1
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.jrf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is a Jamal reference file containing serialized base64 encoded macros
# Created: 2024-12-10 17:58:05 +0100
# Created: 2024-12-11 13:19:58 +0100
# id|openStr|closeStr|verbatim|tailParameter|pure|content|parameters
# TOC
VE9D|eyU=|JX0=|0|0|0|Ci4gPDxJbnN0YWxsYXRpb24+PgouIDw8R1M+PgouIDw8Q29uZmlndXJhdGlvbj4+Ci4gPDxGZWF0dXJlcz4+Ci4gPDxDb250cmlidXRpbmc+PgouIDw8RG9jdW1lbnRhdGlvbj4+Ci4gPDxMaWNlbnNlPj4KLiA8PENoYW5nZWxvZz4+Ci4gPDxSb2FkbWFwPj4KLiA8PFN1cHBvcnQ+PgouIDw8RkFRPj4KLiA8PE1haW50ZW5hbmNlPj4=|
10 changes: 1 addition & 9 deletions jamal-api/src/main/java/javax0/jamal/api/BadSyntaxAt.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,12 @@ public BadSyntaxAt(String message, Position pos, Throwable cause) {
this.pos = pos;
}

public static String posFormat(Position pos) {
if (pos == null) {
return "";
} else {
return pos.file + "/" + pos.line + ":" + pos.column;
}
}

@Override
public String getMessage() {
if (pos == null) {
return super.getMessage();
} else {
return super.getMessage() + " at " + posFormat(pos);
return super.getMessage() + " at " + pos.posFormat();
}
}

Expand Down
3 changes: 1 addition & 2 deletions jamal-api/src/main/java/javax0/jamal/api/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface Input extends CharSequence {
* <li>either perform the operation as described here to support the default implementations, or
* <li>implement the methods in a way that they do not use this method.
* </ul>
*
* <p>
* Get the {@link StringBuilder} that contains the characters of the input. The processing many times works directly
* on the {@link StringBuilder} deleting characters from the start of it as the processing progresses, thus
* essentially modifying/mutating the {@code Input} object.
Expand All @@ -38,7 +38,6 @@ public interface Input extends CharSequence {
*/
Position getPosition();


/**
* @return the reference file name that the input was read from
*/
Expand Down
31 changes: 30 additions & 1 deletion jamal-api/src/main/java/javax0/jamal/api/Position.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package javax0.jamal.api;

import java.util.ArrayList;

/**
* The {@code Position} contains the name of a file, a line number and the column number. This serves as a parameter
* when an error happens. The exception {@link BadSyntaxAt} gets an object of this type as a parameter, and later it is
Expand All @@ -10,19 +12,30 @@
*/
public class Position {
public final String file;
public final ArrayList<String> segment = new ArrayList<>();
public int line;
public int column;
public int charpos = 0;
public final Position parent;

/**
* This is the position that this position is a clone of. This is needed when segments are to be added to or
* removed from a position. The method {@link Input#getPosition()} returns a clone of the position but in the case
* when we want to add a segment we need the original position as we want to modify it.
*/
public final Position cloneOf;

public Position(String file, int line, int column, Position parent) {
this.file = file;
this.line = line;
this.column = column;
this.parent = parent;
this.cloneOf = this;
}


public Position(Position clone) {
cloneOf = clone;
if (clone == null) {
file = null;
line = 1;
Expand Down Expand Up @@ -60,6 +73,14 @@ public Position clone() {
return new Position(this);
}

public void pushSegment(String segment) {
this.segment.add(segment);
}

public void popSegment() {
this.segment.remove(this.segment.size() - 1);
}

/**
* @return return the position, which is the same as the position of the input, but references the position of the
* input as parent. This means that we will have two positions, with the same file/line:column information, but
Expand All @@ -83,12 +104,20 @@ public Position fork() {
*/
public Position top() {
var top = this;
while ( top.parent != null) {
while (top.parent != null) {
top = top.parent;
}
return top;
}

public String posFormat() {
if (segment.isEmpty()) {
return file + "/" + line + ":" + column;
} else {
return file + "[" + String.join(">", segment) + "]" + line + ":" + column;
}
}

@Override
public String toString() {
return file + ":" + line + ":" + column;
Expand Down
2 changes: 1 addition & 1 deletion jamal-cmd/src/main/java/javax0/jamal/cmd/JamalMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void main(String[] args) {
private static final System.Logger LOGGER = System.getLogger("JAMAL");

private static void log(final System.Logger.Level level, final Position pos, final String format, final String... params) {
final var msg = String.format(format, (Object[]) params) + (pos == null ? "" : " at ") + BadSyntaxAt.posFormat(pos);
final var msg = String.format(format, (Object[]) params) + (pos == null ? "" : " at " + pos.posFormat());
LOGGER.log(level, msg);
}

Expand Down
13 changes: 10 additions & 3 deletions jamal-engine/src/main/java/javax0/jamal/engine/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public String process(final Input input) throws BadSyntax {
skipWhiteSpaces(input);
try {
processMacro(input, output);
}catch(LinkageError le){
} catch (LinkageError le) {
throw new BadSyntax("Linkage error", le);
}
} else {
Expand All @@ -215,7 +215,7 @@ public String process(final Input input) throws BadSyntax {
}
processingException = badSyntax;
throw badSyntax;
}finally {
} finally {
closeProcessWithExceptionHandling(output, processingException);
}
traceRecordFactory.dump(null);
Expand Down Expand Up @@ -511,7 +511,14 @@ private void pushBadSyntax(BadSyntax bs, final Position ref) throws BadSyntaxAt
private String evaluateBuiltinMacro(final Input input, final Position ref, final MacroQualifier qualifier) throws BadSyntaxAt {
try {
lastInvokedBuiltInMacro = qualifier.macroId;
return qualifier.macro.evaluate(input, this);
input.getPosition().cloneOf.pushSegment(qualifier.macro.getId());
final String s;
try {
s = qualifier.macro.evaluate(input, this);
} finally {
input.getPosition().cloneOf.popSegment();
}
return s;
} catch (BadSyntax bs) {
pushBadSyntax(bs, ref);
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class JamalLogger {

public static void log(final System.Logger.Level level, final Position pos, final String format, final String... params) {
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("jamal");
final var msg = String.format(format, (Object[]) params) + (pos == null ? "" : " at ") + BadSyntaxAt.posFormat(pos);
final var msg = String.format(format, (Object[]) params) + (pos == null ? "" : " at " + pos.posFormat());
switch (level) {
case DEBUG:
log.debug(msg);
Expand Down
2 changes: 1 addition & 1 deletion jamal-snippet/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3816,7 +3816,7 @@ will result in the output
.output
[source]
----
2024-12-10 17:58:06
2024-12-11 13:19:58
----


Expand Down
Binary file modified jamal-sql/demodb.mv.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private TestThat(Class<? extends Macro> klass) {
private final List<String> logItems = new ArrayList<>();

private void log(final System.Logger.Level level, final Position pos, final String format, final String... params) {
logItems.add("[" + level.getName() + "] " + String.format(format, (Object[]) params) + (pos == null ? "" : " at ") + BadSyntaxAt.posFormat(pos));
logItems.add("[" + level.getName() + "] " + String.format(format, (Object[]) params) + (pos == null ? "" : " at " + pos.posFormat()));
}

public List<String> getLogs() {
Expand Down
6 changes: 4 additions & 2 deletions jamal-tools/src/main/java/javax0/jamal/tools/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/
public class Input implements javax0.jamal.api.Input {
private final StringBuilder input;
private final Position pos;
private Position pos;

Position getPos() {
return pos;
}

/**
* Create an empty input, which may also serve as an output where the characters are collected.
Expand Down Expand Up @@ -147,7 +150,6 @@ public void stepColumn() {
pos.charpos++;
}


@Override
public StringBuilder getSB() {
return input;
Expand Down
Binary file modified jamal-word/src/test/resources/demoConverted.docx
Binary file not shown.
Binary file modified jamal-word/src/test/resources/includetestConverted.docx
Binary file not shown.
Binary file modified jamal-word/src/test/resources/pictureConverted.docx
Binary file not shown.
Binary file modified jamal-word/src/test/resources/sampleConverted.docx
Binary file not shown.

0 comments on commit ec37ee1

Please sign in to comment.