Irony解析C#中的null值

huangapple go评论59阅读模式
英文:

Irony parse null value in c#

问题

I am looking for a way to parse a string (key-value pair) that has null value. I am using Irony nuget in c#.

Example string 1: key = "value"

Example string 2: key = null

I am able to parse Example string 1 using the below way:

var stringLiteral = new StringLiteral("String", "\"");
var filterRule = new BnfExpression();
filterRule |= ((new NonTerminal("Key") { Rule = "Key" }) + (new NonTerminal("ComparisonOp")) + stringLiteral);

I am not quite sure with Example string 2. Is there actually a way to parse nullable values in Irony?

英文:

I am looking for a way to parse a string (key-value pair) that has null value. I am using Irony nuget in c#.

Example string 1: key = "value"

Example string 2: key = null

I am able to parse Example string 1 using the below way:

var stringLiteral = new StringLiteral("String", "\"");
var filterRule = new BnfExpression();
filterRule |= ((new NonTerminal("Key") { Rule = "Key" }) + (new NonTerminal("ComparisonOp")) + stringLiteral);

I am not quite sure with Example string 2. Is there actually a way to parse nullable values in Irony?

答案1

得分: 1

这个正则表达式匹配了"NULL",然后你可以无错误地进行解析。

英文:

I guess I found an answer to this. Posting here so someone with the same requirement can get it.

var nullLiteral = new RegexBasedTerminal("Nullable", "(NULL)");
var filterRule = new BnfExpression();
filterRule |= ((new NonTerminal("Key") { Rule = "Key" }) + (new NonTerminal("ComparisonOp")) + nullLiteral );

This regular expression gets the null match and then you can parse without errors.

huangapple
  • 本文由 发表于 2023年4月11日 03:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定