Skip to content

Commit

Permalink
Improve layout parsing errors
Browse files Browse the repository at this point in the history
Add location information to all error and improve "expected tag" errors.
  • Loading branch information
Julow committed Dec 17, 2023
1 parent 7af6adc commit 478d808
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions srcs/juloo.keyboard2/KeyboardData.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static KeyboardData load_string_exn(String src) throws Exception
private static KeyboardData parse_keyboard(XmlPullParser parser) throws Exception
{
if (!expect_tag(parser, "keyboard"))
throw new Exception("Empty layout file");
throw error(parser, "Expected tag <keyboard>");
boolean add_bottom_row = attribute_bool(parser, "bottom_row", true);
float specified_kw = attribute_float(parser, "width", 0f);
String script = parser.getAttributeValue(null, "script");
Expand All @@ -238,7 +238,7 @@ private static KeyboardData parse_keyboard(XmlPullParser parser) throws Exceptio
modmap = Modmap.parse(parser);
break;
default:
throw new Exception("Unknown tag: " + parser.getName());
throw error(parser, "Expecting tag <row>, got <" + parser.getName() + ">");
}
}
float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows);
Expand All @@ -258,7 +258,7 @@ private static float compute_max_width(List<Row> rows)
private static Row parse_row(XmlPullParser parser) throws Exception
{
if (!expect_tag(parser, "row"))
throw new Exception("Failed to parse row");
throw error(parser, "Expected tag <row>");
return Row.parse(parser);
}

Expand Down Expand Up @@ -604,7 +604,8 @@ private static boolean expect_tag(XmlPullParser parser, String name) throws Exce
if (!next_tag(parser))
return false;
if (!parser.getName().equals(name))
throw new Exception("Unknown tag: " + parser.getName());
throw error(parser, "Expecting tag <" + name + ">, got <" +
parser.getName() + ">");
return true;
}

Expand All @@ -623,4 +624,10 @@ private static float attribute_float(XmlPullParser parser, String attr, float de
return default_val;
return Float.parseFloat(val);
}

/** Construct a parsing error. */
private static Exception error(XmlPullParser parser, String message)
{
return new Exception(message + " " + parser.getPositionDescription());
}
}

0 comments on commit 478d808

Please sign in to comment.