好吧,虽然这不是我所想的是最好的方式,由于所需的权宜之计,我已经通过如下创建自定义令牌和令牌工厂奉行这样的:
在解析命名空间
,我创建了一个自定义的令牌:
public class VToken : CommonToken
{
public IModelTreeNode ModelTreeNode;
public bool IsRecognized;
public string[] Completions;
public VToken(int type, String text) : base(type, text)
{
}
public VToken(Tuple<ITokenSource, ICharStream> source, int type,
int channel, int start, int stop) : base(source, type, channel, start, stop)
{
}
public override string ToString()
{
return "VToken : " + Text;
}
}
然后我创建了一个自定义的令牌工厂(也是在解析命名空间)
public class VTokenFactory : ITokenFactory
{
public static readonly ITokenFactory Default = (ITokenFactory)new VTokenFactory();
protected internal readonly bool copyText;
public VTokenFactory(bool copyText)
{
this.copyText = copyText;
}
public VTokenFactory()
: this(false)
{
}
public virtual VToken Create(Tuple<ITokenSource, ICharStream> source, int type, string text, int channel, int start, int stop, int line, int charPositionInLine)
{
VToken myToken = new VToken(source, type, channel, start, stop)
{
Line = line,
Column = charPositionInLine
};
if (text != null)myToken.Text = text;
else if (this.copyText && source.Item2 != null) myToken.Text = source.Item2.GetText(Interval.Of(start, stop));
return myToken;
}
IToken ITokenFactory.Create(Tuple<ITokenSource, ICharStream> source, int type, string text, int channel, int start, int stop, int line, int charPositionInLine)
{
return (IToken)this.Create(source, type, text, channel, start, stop, line, charPositionInLine);
}
public virtual VToken Create(int type, string text)
{
return new VToken(type, text);
}
IToken ITokenFactory.Create(int type, string text)
{
return (IToken)this.Create(type, text);
}
}
最后,改变解析器创建如下:
VLexer lexer = new VLexer(input);
VTokenFactory factory = new VTokenFactory(); // <-- added
lexer.TokenFactory = factory; // <-- added
CommonTokenStream tokens = new CommonTokenStream(lexer);
VParser parser = new VParser(tokens);
,当我处理规则,我通过访问新的领域:
((VToken)context.children[0].Payload).Completions
((VToken)context.children[0].Payload).IsRecognized
((VToken)context.children[0].Payload).ModelTreeNode
注:本作品,但正如我所说的,感觉并不像它的建筑的“正确”的地方把它。任何建议仍然是非常受欢迎的。 – 2015-02-11 23:29:41