Skip to content

Commit

Permalink
yang-tool: print out syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Dec 19, 2023
1 parent 3ab6cfb commit 6347e9b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,23 @@ public ModuleData getEntryModule() {
return null;
}

public void addError(String moduleFile, EObject source, String message) {
public void addError(String moduleFile, int line, int col, String message) {
var msgEntry = new MessageEntry();
msgEntry.moduleFile = moduleFile == null ? "<unknown>" : moduleFile;
msgEntry.line = -1;
msgEntry.line = line;
msgEntry.col = col;
msgEntry.severity = Severity.Error;
msgEntry.message = message;
messages.add(msgEntry);
}

public void addError(String moduleFile, EObject source, String message) {
var line = -1;
ICompositeNode node = NodeModelUtils.getNode(source);
if (node != null) {
msgEntry.line = node.getStartLine();
line = node.getStartLine();
}
messages.add(msgEntry);
addError(moduleFile, line, -1, message);
}

public List<MessageEntry> getMessages() {
Expand Down Expand Up @@ -443,13 +449,13 @@ public void addToChildren(HasStatements child) {

public static class MessageEntry {
String moduleFile;
int line;
int line, col = -1;
Severity severity;
String message;

@Override
public String toString() {
return moduleFile + ":" + line + ": " + severity + ": " + message;
return moduleFile + ":" + line + ":" + (col < 0 ? "" : col) + " " + severity + ": " + message;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ protected ProcessedDataModel processInternal(List<AbstractModule> modules, List<
List<String> excludedFeatures) {
var evalCtx = new FeatureEvaluationContext(includedFeatures, excludedFeatures);
ProcessedDataModel processedModel = new ProcessedDataModel();
collectSyntaxErrors(modules.get(0), processedModel);

modules.forEach((module) -> module.eAllContents().forEachRemaining((ele) -> {
if (ele instanceof Deviate) {
processDeviate((Deviate) ele, module, processedModel);
Expand All @@ -113,6 +115,13 @@ protected ProcessedDataModel processInternal(List<AbstractModule> modules, List<
return processedModel;
}

private void collectSyntaxErrors(AbstractModule entryModule, ProcessedDataModel processedModel) {
var moduleFile = moduleFileName(entryModule);
entryModule.eResource().getErrors().forEach(diagnostic -> {
processedModel.addError(moduleFile, diagnostic.getLine(), diagnostic.getColumn(), diagnostic.getMessage());
});
}

/*
* The deviates's Substatements: config, default, mandatory, max-elements,
* min-elements, must, type, unique, units. Properties 'must' and 'unique' are
Expand All @@ -121,7 +130,7 @@ protected ProcessedDataModel processInternal(List<AbstractModule> modules, List<
protected void processDeviate(Deviate deviate, AbstractModule module, ProcessedDataModel processedModel) {
var deviation = (Deviation) deviate.eContainer();
SchemaNode targetNode = deviation.getReference().getSchemaNode();
if(targetNode == null || targetNode.eIsProxy()) {
if (targetNode == null || targetNode.eIsProxy()) {
processedModel.addError(moduleFileName(module), deviation.getReference(),
"Deviation target node not found");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class DeviationTest extends AbstractYangTest {
}
}
}
'''.load().root
'''.load.root

val deviateModule = '''
module example-deviations {
Expand Down Expand Up @@ -251,14 +251,63 @@ class DeviationTest extends AbstractYangTest {
val processor = new YangProcessor()
val processedData = processor.process(#[mainModule, deviateModule], null, null)
assertEquals(4, processedData.messages.size)
assertEquals('__synthetic1.yang:18: Error: the "default" property already exists in node "base-test-module:system:user:type"',
assertEquals(
'__synthetic1.yang:18: Error: the "default" property already exists in node "base-test-module:system:user:type"',
processedData.messages.head.toString)
assertEquals('__synthetic1.yang:25: Error: the "max-elements" property does not exist in node "base-test-module:system:name-server"',
assertEquals(
'__synthetic1.yang:25: Error: the "max-elements" property does not exist in node "base-test-module:system:name-server"',
processedData.messages.get(1).toString)
assertEquals('__synthetic1.yang:32: Error: the "must" property does not exist in node "base-test-module:system"',
assertEquals(
'__synthetic1.yang:32: Error: the "must" property does not exist in node "base-test-module:system"',
processedData.messages.get(2).toString)
assertEquals('__synthetic1.yang:41: Error: Deviation target node not found',
processedData.messages.get(3).toString)
}

@Test
def void testSyntaxErrorsProcessing() {

val mainModule = '''
module base-test-module {
yang-version 1.1;
namespace urn:ietf:params:xml:ns:yang:base-test-module;
prefix base-test-module;
container system {
must "user";
container daytime {
leaf date {
type string;
}
}
le af time {
type string;
}
container user {
leaf type {
default "normal"; // error on "add" cause already exists
type string {
length "1..10";
}
}
}
leaf-list name-server {
type string;
}
}
}
'''.loadWithSyntaxErrors().root
val processor = new YangProcessor()
val processedData = processor.process(#[mainModule], null, null)
assertEquals(2, processedData.messages.size)
assertEquals(
"__synthetic0.yang:15:9 Error: mismatched input 'le' expecting '}'",
processedData.messages.head.toString)
assertEquals(
"__synthetic0.yang:19:9 Error: missing EOF at 'container'",
processedData.messages.get(1).toString)
}
}

0 comments on commit 6347e9b

Please sign in to comment.