如何在其他程序代码中使用文本格式?
这个问题听起来微不足道,但让我解释一下我的情况。
我正在使用面向对象的编程语言(C#),大多数实际执行代码是过程,即一系列语句,有时是分支和循环。相当标准。
现在我面临一项处理文本格式的任务(PGN,但它可以是任何东西其他如 VCard 或某些自定义格式)。至少对我来说,使用它的“标准”方法是混合使用:
- 正则表达式
- if / switch 语句
- for 循环
- 将正则表达式匹配存储到某些自定义结构中和/或将其输出为某种结果格式
但是,我根本不喜欢这种过程方法 - 正则表达式容易出错,代码通常很难理解和调试,它通常具有相当高的圈复杂度等。
简单地说,我希望它是声明式但我不知道该使用什么工具或库。
我记得当我看到“M”语言的演示时,我认为这正是我正在寻找的。有一种简单的方法来声明我的文本格式的语法,然后该工具会自动将输入字符串解析为文本 DSL 的内存中表示形式,我认为也可以将格式转换为另一种格式等
。还与 JetBrains MPS 背后的人员保持联系,这是另一个工作工具使用 DSL,但我的场景没有似乎与他们试图提供的内容完美匹配。
因此,如果有人知道如何在其他程序代码库中优雅地处理文本格式,我很乐意了解这些选项。
This question sounds trivial but let me explain my scenario.
I am working in an object oriented programming language (C#) and most of the actual execution code is procedural, i.e. series of statements, sometimes branches and loops. Fairly standard.
Now I am presented with a task to deal with a textual format (PGN, but it could be anything other like VCard or some custom format). At least for me, the "standard" way to work with it would be to use a mix of:
- regular expressions
- if / switch statements
- for-loops
- storing regexp matches into some custom structure and / or outputting it to some result format
However, I don't like this procedural approach at all - regular expressions are prone to errors, the code is usually quite hard to understand and debug, it usually tends to have quite a high cyclomatic complexity etc.
Simply put, I'd like it to be declarative but I don't know what tools or libraries to use.
I remember that when I saw demos of the "M" language I thought that that was exactly I was looking for. There was a simple way to declare syntax of my textual format, the tool would then automatically parse input string into an in-memory representation of the textual DSL, I think that it was also possible to transform the format into another etc.
I have been also in touch with the people behind JetBrains MPS which is another tool for working with DSLs but my scenario doesn't seem to be a perfect match for what they are trying to provide.
So if anyone has any idea about how to elegantly deal with textual formats in otherwise procedural code base, I'd be happy to learn about the options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看我的开源项目 meta#。我认为这听起来正是您正在寻找的。
Check out my open source project meta#. I think it sounds like exactly what you're looking for.