1

我正在尝试解析散布有函数的对象,并且我想跳过这些函数。

结构是:

/datum/chemical_reaction/reagent_explosion
    name = "Generic explosive"
    id = "reagent_explosion"
    var/strengthdiv = 10
    var/modifier = 0

/datum/chemical_reaction/reagent_explosion/on_reaction(datum/reagents/holder, created_volume)
    var/power = modifier + round(created_volume/strengthdiv, 1)
    if(power > 0)
        var/turf/T = get_turf(holder.my_atom)
        var/inside_msg
        if(ismob(holder.my_atom))
        // More code ...

/datum/chemical_reaction/reagent_explosion/nitroglycerin
    name = "Nitroglycerin"
    id = /datum/reagent/nitroglycerin
    results = list(/datum/reagent/nitroglycerin = 2)
    required_reagents = list(/datum/reagent/glycerol = 1, /datum/reagent/toxin/acid/nitracid = 1, /datum/reagent/toxin/acid = 1)
    strengthdiv = 2

/datum/chemical_reaction/reagent_explosion/nitroglycerin/on_reaction(datum/reagents/holder, created_volume)
    if(holder.has_reagent(/datum/reagent/stabilizing_agent))
        return
    holder.remove_reagent(/datum/reagent/nitroglycerin, created_volume*2)
    ..()

一个函数可以被路径之后的paranthasees识别。由于解析代码很困难,我只想在开括号之后跳过令牌,直到找到一个 EOL 令牌,然后是一个 ReferencePath。我试图解析函数,但没有真正的回报太难了,所以这就是我目前所处的位置:

public static readonly TokenListParser<DMToken, string[]> ParameterDefinition =
    from _ in Token.EqualTo(DMToken.OpenParen)
    from values in Identifier.ManyDelimitedBy(Token.EqualTo(DMToken.Comma))
    from __ in Token.EqualTo(DMToken.CloseParen)
    select values;

public static readonly TokenListParser<DMToken, object> Function =
    from path in ReferencePath
    from _ in ParameterDefinition
    from __ in Token.EqualTo(DMToken.Eol)
    from ___ in LoneStatement.Many()
    from ____ in Token.EqualTo(DMToken.ReturnKeyword)
    select null as object;

public static readonly TokenListParser<DMToken, ExpandoObject> Object =
    from path in ReferencePath
    from _ in Token.EqualTo(DMToken.Eol)
    from values in LoneAssignment.Many().Select(a => {
            // KeyValuePair to dynamic mapping code
        })
    select values;

public static readonly TokenListParser<DMToken, ExpandoObject[]> ObjectList =
    (from obj in Object//.Try().Or(Function.Select(x => (dynamic)x))
     from _ in Token.EqualTo(DMToken.Eol).Many()
     select obj).Many();

源代码,虽然不是最新版本:https ://github.com/RedlineTriad/DMParser

我试图解析的代码:https ://github.com/tgstation/tgstation/tree/master/code/modules/reagents/chemistry/recipes

4

0 回答 0