Using a DSL to generate C# Code

前端 未结 3 601
面向向阳花
面向向阳花 2021-02-09 19:00

Currently the project I\'m working with does not have completely fixed models (due to an external influence) and hence I\'d like some flexibility in writing them. Currently they

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-09 19:17

    I think you are on the right track.

    What I usually do in a situation like this is design a simple language that captures my needs and write a LL1 (Recursive Descent) parser for it.

    If the language has to have non-trivial C# syntax in it, I can either quote that, or just wrap it in brackets that I can recognize, and just pass it through to the output code.

    I can either have it generate a parse tree structure, and generate say 3 different kinds of code from that, or I can just have it generate code on the fly, either using a mode variable with 3 values, or just simultaneously write code to 3 different output files.

    There's more than one way to do it. If you are afraid of writing parsers (as some programmers are), there is lots of help elsewhere on SO.

提交回复
热议问题