-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathPhiSyntax.java
More file actions
187 lines (176 loc) · 5.71 KB
/
Copy pathPhiSyntax.java
File metadata and controls
187 lines (176 loc) · 5.71 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.parser;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import java.util.function.Function;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.cactoos.Text;
import org.cactoos.io.InputStreamOf;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Unchecked;
import org.xembly.Directive;
import org.xembly.Directives;
import org.xembly.Xembler;
/**
* Syntax parser that converts Phi-calculus notation to XMIR (XML-based Intermediate Representation)
* using ANTLR4. PhiSyntax parses Phi-calculus code and transforms it into structured XML
* that represents the program in a normalized form.
*
* <p>The Phi-calculus is a formal model of computation that serves as the theoretical foundation
* for the EO language. This parser converts Phi-calculus notation with its specialized
* symbols (like ⟦, ⟧, ↦, ⤍, etc.) into XMIR that can be further processed.</p>
*
* <p>The parsing process includes lexical analysis, syntax analysis, and XML transformations:
* 1. Phi code is tokenized by the PhiLexer
* 2. Then parsed by the ANTLR-generated PhiParser
* 3. Finally transformed into canonical XMIR through a series of XSL transformations</p>
*
* <p>Usage examples:</p>
*
* <p>1. Parse basic Phi-calculus notation:</p>
* <pre>
* XML xmir = new PhiSyntax("{⟦obj ↦ ⟦x ↦ Φ.org.eolang.int⟧⟧}").parsed();
* </pre>
*
* <p>2. Parse with a program name and add extra directives:</p>
* <pre>
* XML xmir = new PhiSyntax(
* "factorial",
* () -> "{⟦fact ↦ ⟦n ↦ Φ.org.eolang.int⟧⟧}",
* new Directives().xpath("/object").attr("version", "1.0")
* ).parsed();
* </pre>
*
* <p>3. Parse with custom transformations:</p>
* <pre>
* XML xmir = new PhiSyntax(
* "{⟦x ↦ ⟦y ↦ Φ.org.eolang.float⟧⟧}",
* new TrClasspath<>(
* "/org/eolang/parser/unphi/specialized-transform.xsl"
* )
* ).parsed();
* </pre>
*
* <p>After parsing, any syntax errors can be found in the XML at the "/object/errors" XPath.
* If no errors are present, the parsed program is valid Phi-calculus notation.</p>
*
* @since 0.34.0
*/
public final class PhiSyntax implements Syntax {
/**
* Set of optimizations that builds canonical XMIR from parsed PHI.
*/
private static final Function<XML, XML> CANONICAL = new Xsline(
new TrFull(
new TrClasspath<>(
"/org/eolang/parser/parse/move-voids-up.xsl",
"/org/eolang/parser/parse/wrap-method-calls.xsl",
"/org/eolang/parser/parse/validate-objects-count.xsl",
"/org/eolang/parser/unphi/wrap-bytes.xsl",
"/org/eolang/parser/parse/roll-bases.xsl"
).back()
)
)::pass;
/**
* Input.
*/
private final Text input;
/**
* Extra directives to append to parsed phi.
*/
private final Iterable<Directive> extra;
/**
* Transform XMIR after parsing.
*/
private final Function<XML, XML> transform;
/**
* Ctor for the tests.
* @param input Input
*/
public PhiSyntax(final String input) {
this(() -> input);
}
/**
* Ctor for testing.
* @param input Input
* @param train Train of transformations to apply
*/
PhiSyntax(final String input, final Train<Shift> train) {
this(() -> input, new Directives(), new Xsline(train)::pass);
}
/**
* Ctor for the tests.
* @param input Input
*/
public PhiSyntax(final Text input) {
this(input, new Directives());
}
/**
* Ctor.
* @param inpt Input
* @param extra Extra directives to append
*/
public PhiSyntax(final Text inpt, final Iterable<Directive> extra) {
this(inpt, extra, CANONICAL);
}
/**
* Base ctor.
* @param inpt Input
* @param extra Extra directives to append
* @param transform Functions that transforms XMIR after parsing
*/
private PhiSyntax(
final Text inpt,
final Iterable<Directive> extra,
final Function<XML, XML> transform
) {
this.input = inpt;
this.extra = extra;
this.transform = transform;
}
@Override
public XML parsed() throws IOException {
final XePhiListener xel = new XePhiListener();
final GeneralErrors spy = new GeneralErrors(this.input);
final PhiLexer lexer = new PhiLexer(
CharStreams.fromStream(
new InputStreamOf(this.input)
)
);
lexer.addErrorListener(spy);
final PhiParser parser = new PhiParser(
new CommonTokenStream(lexer)
);
parser.removeErrorListeners();
parser.addErrorListener(spy);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = this.transform.apply(
new XMLDocument(
new Xembler(
new Directives(xel).append(new DrErrors(spy)).append(this.extra)
).domQuietly()
)
);
final long errors = new Unchecked<>(new LengthOf(spy)).value();
if (errors == 0) {
Logger.debug(this, "Input of PHI calculus compiled, no errors");
} else {
Logger.debug(
this, "Input of PHI calculus failed to compile (%d errors)",
errors
);
}
return dom;
}
}