Permalink
Browse files

extract GrammarMapper

  • Loading branch information...
1 parent 8fe29b9 commit a80f9eee5c50c6ba531e5f9ccef8d27863d82672 @inspirer committed Mar 25, 2013
@@ -20,6 +20,7 @@
import org.textmapper.lapg.api.ParserData;
import org.textmapper.lapg.api.ProcessingStatus;
import org.textmapper.lapg.api.builder.GrammarBuilder;
+import org.textmapper.lapg.api.builder.GrammarMapper;
import org.textmapper.lapg.api.regex.RegexContext;
import org.textmapper.lapg.api.regex.RegexMatcher;
import org.textmapper.lapg.api.regex.RegexParseException;
@@ -40,6 +41,10 @@ public static GrammarBuilder createBuilder() {
return GrammarFacade.createBuilder();
}
+ public static GrammarMapper createMapper(Grammar g) {
+ return GrammarFacade.createMapper(g);
+ }
+
public static ParserData generateParser(Grammar g, ProcessingStatus status) {
return Builder.compile(g, status);
}
@@ -1,11 +0,0 @@
-package org.textmapper.lapg.api;
-
-/**
- * evgeny, 2/3/13
- */
-public interface AnnotatedSourceElement extends SourceElement {
-
- SourceAnnotation getAnnotation(String name);
-
- SourceAnnotation[] getAnnotations();
-}
@@ -1,24 +0,0 @@
-/**
- * Copyright 2002-2013 Evgeny Gryaznov
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.textmapper.lapg.api;
-
-/**
- * evgeny, 2/3/13
- */
-public interface SourceAnnotation {
-
- String getName();
-}
@@ -16,16 +16,12 @@
package org.textmapper.lapg.api.builder;
import org.textmapper.lapg.api.*;
-import org.textmapper.lapg.api.ast.AstEnumMember;
-import org.textmapper.lapg.api.ast.AstField;
-import org.textmapper.lapg.api.ast.AstModel;
-import org.textmapper.lapg.api.ast.AstType;
import org.textmapper.lapg.api.regex.RegexPart;
import org.textmapper.lapg.api.rule.*;
import java.util.Collection;
-public interface GrammarBuilder {
+public interface GrammarBuilder extends GrammarMapper {
Terminal addTerminal(String name, String type, SourceElement origin);
@@ -69,11 +65,5 @@
Prio addPrio(int prio, Collection<Terminal> symbols, SourceElement origin);
- void link(AstModel ast);
-
- void map(Symbol symbol, AstType type);
-
- void map(RhsSymbol symbol, AstField field, AstEnumMember value, boolean isAddition);
-
Grammar create();
}
@@ -0,0 +1,17 @@
+package org.textmapper.lapg.api.builder;
+
+import org.textmapper.lapg.api.Symbol;
+import org.textmapper.lapg.api.ast.AstEnumMember;
+import org.textmapper.lapg.api.ast.AstField;
+import org.textmapper.lapg.api.ast.AstType;
+import org.textmapper.lapg.api.rule.RhsSymbol;
+
+/**
+ * evgeny, 3/25/13
+ */
+public interface GrammarMapper {
+
+ void map(Symbol symbol, AstType type);
+
+ void map(RhsSymbol symbol, AstField field, AstEnumMember value, boolean isAddition);
+}
@@ -10,6 +10,9 @@
AstField getField();
+ /**
+ * Only for fields with AstEnum type.
+ */
AstEnumMember getValue();
boolean isAddition();
@@ -15,15 +15,21 @@
*/
package org.textmapper.lapg.builder;
+import org.textmapper.lapg.api.Grammar;
import org.textmapper.lapg.api.builder.AstBuilder;
import org.textmapper.lapg.api.builder.GrammarBuilder;
+import org.textmapper.lapg.api.builder.GrammarMapper;
public class GrammarFacade {
public static GrammarBuilder createBuilder() {
return new LiGrammarBuilder();
}
+ public static GrammarMapper createMapper(Grammar grammar) {
+ return new LiGrammarMapper(grammar);
+ }
+
public static AstBuilder createAstBuilder() {
return new LiAstBuilder();
}
@@ -16,7 +16,6 @@
package org.textmapper.lapg.builder;
import org.textmapper.lapg.api.*;
-import org.textmapper.lapg.api.ast.*;
import org.textmapper.lapg.api.builder.GrammarBuilder;
import org.textmapper.lapg.api.regex.RegexPart;
import org.textmapper.lapg.api.rule.*;
@@ -26,11 +25,10 @@
/**
* evgeny, 14.12.11
*/
-class LiGrammarBuilder implements GrammarBuilder {
+class LiGrammarBuilder extends LiGrammarMapper implements GrammarBuilder {
private final Map<String, LiSymbol> symbolsMap = new HashMap<String, LiSymbol>();
- private final Set<Symbol> symbolsSet = new HashSet<Symbol>();
private final List<LiSymbol> symbols = new ArrayList<LiSymbol>();
private final List<LiLexerRule> lexerRules = new ArrayList<LiLexerRule>();
private final List<LiNamedPattern> namedPatterns = new ArrayList<LiNamedPattern>();
@@ -41,13 +39,12 @@
private final List<LiPrio> priorities = new ArrayList<LiPrio>();
private final Set<RhsPart> rhsSet = new HashSet<RhsPart>();
private final Set<Terminal> sealedTerminals = new HashSet<Terminal>();
- private final Set<AstField> knownFields = new HashSet<AstField>();
- private AstModel astModel;
private final List<LiInputRef> inputs = new ArrayList<LiInputRef>();
private final Terminal eoi;
public LiGrammarBuilder() {
+ super(null);
eoi = addTerminal(Symbol.EOI, null, null);
}
@@ -306,6 +303,7 @@ public RhsList list(RhsPart inner, RhsPart separator, boolean nonEmpty, SourceEl
return result;
}
+ @Override
void check(RhsPart part, boolean asChild) {
if (part == null) {
throw new NullPointerException();
@@ -318,77 +316,6 @@ void check(RhsPart part, boolean asChild) {
}
}
-
- void check(AstField field) {
- if (field == null) {
- throw new NullPointerException();
- }
- if (!knownFields.contains(field)) {
- throw new IllegalArgumentException("unknown ast field passed");
- }
- }
-
- void check(Symbol sym) {
- if (sym == null) {
- throw new NullPointerException();
- }
- if (!symbolsSet.contains(sym)) {
- throw new IllegalArgumentException("unknown symbol passed");
- }
- }
-
- @Override
- public void link(AstModel ast) {
- if (astModel != null) {
- throw new IllegalStateException("cannot re-link ast model");
- }
- astModel = ast;
- for (AstClass cl : astModel.getClasses()) {
- link(cl);
- }
- }
-
- private void link(AstClass cl) {
- Collections.addAll(knownFields, cl.getFields());
- for (AstClass inner : cl.getInner()) {
- link(inner);
- }
- }
-
- @Override
- public void map(Symbol symbol, AstType type) {
- check(symbol);
- // TODO check(type);
- ((LiSymbol) symbol).setMapping(type);
- }
-
- @Override
- public void map(RhsSymbol symbol, AstField field, AstEnumMember value, boolean isAddition) {
- check(symbol, false);
- check(field);
- final AstType nontermType = symbol.getLeft().getMapping();
- if (nontermType == null) {
- throw new IllegalArgumentException("cannot map symbol, map nonterminal first");
- }
- if (field != null && nontermType != field.getContainingClass()) {
- throw new IllegalArgumentException("field should be from the nonterminal class");
- }
- AstType type = field != null ? field.getType() : nontermType;
- if (isAddition) {
- if (!(type instanceof AstList)) {
- throw new IllegalArgumentException("addition is applicable only to list types");
- }
- type = ((AstList) type).getInner();
- }
- if (value != null && type != value.getContainingEnum()) {
- throw new IllegalArgumentException(
- "enumeration value should match " + (field != null
- ? "the field type"
- : "the nonterminal type"));
- }
- ((LiRhsSymbol) symbol).setMapping(new LiRhsMapping(field, value, isAddition));
- }
-
@Override
public Grammar create() {
LiSymbol[] symbolArr = symbols.toArray(new LiSymbol[symbols.size()]);
@@ -0,0 +1,95 @@
+/**
+ * Copyright 2002-2013 Evgeny Gryaznov
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.textmapper.lapg.builder;
+
+import org.textmapper.lapg.api.Grammar;
+import org.textmapper.lapg.api.Symbol;
+import org.textmapper.lapg.api.ast.AstEnumMember;
+import org.textmapper.lapg.api.ast.AstField;
+import org.textmapper.lapg.api.ast.AstList;
+import org.textmapper.lapg.api.ast.AstType;
+import org.textmapper.lapg.api.builder.GrammarMapper;
+import org.textmapper.lapg.api.rule.RhsPart;
+import org.textmapper.lapg.api.rule.RhsSymbol;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+class LiGrammarMapper implements GrammarMapper {
+
+ protected final Set<Symbol> symbolsSet = new HashSet<Symbol>();
+
+ LiGrammarMapper(Grammar grammar) {
+ if (grammar != null) {
+ symbolsSet.addAll(Arrays.asList(grammar.getSymbols()));
+ }
+ }
+
+ void check(RhsPart part, boolean asChild) {
+ if (part == null || part.getLeft() == null) {
+ throw new NullPointerException();
+ }
+ if (!symbolsSet.contains(part.getLeft())) {
+ throw new IllegalArgumentException("unknown right-hand side element passed");
+ }
+ }
+
+
+ final void check(Symbol sym) {
+ if (sym == null) {
+ throw new NullPointerException();
+ }
+ if (!symbolsSet.contains(sym)) {
+ throw new IllegalArgumentException("unknown symbol passed");
+ }
+ }
+
+ @Override
+ public void map(Symbol symbol, AstType type) {
+ check(symbol);
+ if (symbol.getMapping() != null) {
+ throw new IllegalArgumentException("cannot re-map symbol");
+ }
+ ((LiSymbol) symbol).setMapping(type);
+ }
+
+ @Override
+ public void map(RhsSymbol symbol, AstField field, AstEnumMember value, boolean isAddition) {
+ check(symbol, false);
+ final AstType nontermType = symbol.getLeft().getMapping();
+ if (nontermType == null) {
+ throw new IllegalArgumentException("cannot map symbol, map nonterminal first");
+ }
+ if (field != null && nontermType != field.getContainingClass()) {
+ throw new IllegalArgumentException("field should be from the nonterminal class");
+ }
+ AstType type = field != null ? field.getType() : nontermType;
+ if (isAddition) {
+ if (!(type instanceof AstList)) {
+ throw new IllegalArgumentException("addition is applicable only to list types");
+ }
+ type = ((AstList) type).getInner();
+ }
+ if (value != null && type != value.getContainingEnum()) {
+ throw new IllegalArgumentException(
+ "enumeration value should match " + (field != null
+ ? "the field type"
+ : "the nonterminal type"));
+ }
+ ((LiRhsSymbol) symbol).setMapping(new LiRhsMapping(field, value, isAddition));
+ }
+}
@@ -1,16 +1,20 @@
package org.textmapper.tool.compiler;
+import org.textmapper.lapg.LapgCore;
import org.textmapper.lapg.api.Grammar;
+import org.textmapper.lapg.api.builder.GrammarMapper;
/**
* evgeny, 1/29/13
*/
public class TMMapper {
private final Grammar grammar;
+ private final GrammarMapper mapper;
public TMMapper(Grammar grammar) {
this.grammar = grammar;
+ this.mapper = LapgCore.createMapper(grammar);
}
public void mapAST() {
@@ -20,12 +20,6 @@
public interface IAstNode extends TextSourceElement {
- int getLine();
-
- int getOffset();
-
- int getEndOffset();
-
TextSource getInput();
void accept(AbstractVisitor v);

0 comments on commit a80f9ee

Please sign in to comment.