-
Notifications
You must be signed in to change notification settings - Fork 9
/
CompileExtensionPostfix.n
88 lines (80 loc) · 2.82 KB
/
CompileExtensionPostfix.n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;
using System;
using System.Linq;
using SCG = System.Collections.Generic;
using Nemerle.Parser.Internal;
namespace Nemerle.Parser
{
partial internal class RuleCompiler
{
public CompileExtensionPostfix(rd : RuleDefinition.ExtentionPostfix) : void
{
def className = _grammarCompiller.DecorateRuleClassName(rd);
def returnType = _grammarCompiller.GetBaseRuleNType(rd);
def makeBody(useResult)
{
def body = if (useResult)
{
def (varNames, setResult) = MakeHandlerCall(rd.rule);
def subRulesCode = CompileRule(rd.rule, varNames);
DefVars(varNames, <[
mutable prefixResult = result;
def newPos = $subRulesCode;
if (newPos > 0)
$setResult;
else
$(SetParsingErrorCode($"#$(rd.Name)"));
newPos;
]>)
}
else
<[
$(CompileRule(rd.rule, []))
]>;
<[
unchecked // востановление после ошибок тут не нужно
{ // оно будет произведено в точке расширения
mutable c : char;
_ = c; // eliminate warning "a local value c was never used"
mutable isBest = false;
$body;
}
]>
}
def implClass = <[decl:
public class $(className : dyn) : ExtensionPostfixBase[$(returnType : typed)]
{
private _grammar : $(_grammarCompiller.GrammarImplTB.ParsedTypeName);
public this(grammar : IGrammar)
{
base($(rd.bindingPower : int), char.MinValue, char.MaxValue);//TODO:FIXME
_grammar = grammar :> $(_grammarCompiller.GrammarImplTB.ParsedTypeName);
}
public override Descriptor : RuleDescriptor { get { GrammarDescriptorImpl.$(className : dyn).StaticDescriptor } }
}
]>;
def tb = _grammarCompiller.GrammarImplTB.DefineNestedType(implClass);
//_ = tb.DefineWithSource(
// <[decl:
// public override Parse(pos : int, text : string) : int
// {
// $(makeBody(false))
// }
// ]>);
_ = tb.DefineWithSource(
<[decl:
public override Parse(startPos : int, pos : int, text : string, bestOffsets : array[int], result : ref $(returnType : typed)) : int
{
_ = startPos; //TODO: узнать у хардкейса что там за химия с локейшенами
$(makeBody(true))
}
]>);
tb.Compile();
}
}
}