Permalink
Browse files

Remove syntax extension stuff.

There's now just a global grammar like a normal language.
  • Loading branch information...
1 parent c23d85d commit 9de20ae3616a2da459232cfce9badaf63933b1bc @munificent committed Feb 25, 2012
@@ -31,7 +31,7 @@ public Repl createRepl() {
}
public void defineMethod(String signature, String doc, Method method) {
- MagpieParser parser = MagpieParser.create(signature);
+ MagpieParser parser = new MagpieParser(signature);
Pair<String, Pattern> parsed = parser.parseSignature();
String name = parsed.getKey();
@@ -14,11 +14,10 @@
}
public ReplResult readAndEvaluate(SourceReader reader) {
- MagpieParser parser = MagpieParser.create(reader,
- mInterpreter.getBaseModule().getGrammar());
+ MagpieParser parser = new MagpieParser(reader);
try {
- Expr expr = parser.parseExpression();
+ Expr expr = parser.parseStatement();
parser.consume(TokenType.LINE);
Obj result = mInterpreter.interpret(expr);
@@ -38,9 +37,5 @@ public ReplResult readAndEvaluate(SourceReader reader) {
}
}
- public boolean isKeyword(String name) {
- return mInterpreter.getBaseModule().getGrammar().isKeyword(name);
- }
-
private final Interpreter mInterpreter;
}
@@ -63,8 +63,11 @@ protected void afterReadLine(Repl repl, String prompt, String line) {
token.isKeyword("it")) {
// special identifiers
Term.set(ForeColor.LIGHT_BLUE);
+ // TODO(bob): Fix this once we have token types for the reserved words.
+ /*
} else if (repl.isKeyword(token.getString())) {
Term.set(ForeColor.CYAN);
+ */
} else {
Term.set(ForeColor.WHITE);
}
@@ -252,7 +252,7 @@ public void finishInitialization() {
}
private void evaluateModule(Module module) {
- MagpieParser parser = module.createParser();
+ MagpieParser parser = new MagpieParser(module.readSource());
mLoadingModules.push(module);
try {
@@ -264,20 +264,16 @@ private void evaluateModule(Module module) {
}
}
- // Evaluate every expression in the file. We do this incrementally so
- // that expressions that define parsers can be used to parse the rest of
- // the file.
- while (true) {
- try {
- Expr expr = parser.parseTopLevelExpression();
- if (expr == null) break;
+ // Evaluate the module.
+ try {
+ List<Expr> exprs = parser.parseModule();
+ for (Expr expr : exprs) {
evaluate(expr, module, module.getScope());
- } catch (ParseException e) {
- String message = String.format("Syntax error at %s: %s",
- e.getPosition(), e.getMessage());
- mHost.showSyntaxError(message);
- break;
}
+ } catch (ParseException e) {
+ String message = String.format("Syntax error at %s: %s",
+ e.getPosition(), e.getMessage());
+ mHost.showSyntaxError(message);
}
} finally {
mLoadingModules.pop();
@@ -10,10 +10,6 @@
import com.stuffwithstuff.magpie.SourceFile;
import com.stuffwithstuff.magpie.ast.Expr;
import com.stuffwithstuff.magpie.ast.FnExpr;
-import com.stuffwithstuff.magpie.parser.Grammar;
-import com.stuffwithstuff.magpie.parser.InfixParser;
-import com.stuffwithstuff.magpie.parser.MagpieParser;
-import com.stuffwithstuff.magpie.parser.PrefixParser;
import com.stuffwithstuff.magpie.parser.StringReader;
public class Module implements Context {
@@ -28,32 +24,15 @@ public Module(String name, SourceFile info, Interpreter interpreter) {
public Interpreter getInterpreter() { return mInterpreter; }
public Scope getScope() { return mScope; }
public Set<String> getExportedNames() { return mExportedNames; }
- public Grammar getGrammar() { return mGrammar; }
- private SourceReader readSource() {
+ public SourceReader readSource() {
return new StringReader(mInfo.getPath(), mInfo.getSource());
}
public void export(String name) {
mExportedNames.add(name);
}
- public MagpieParser createParser() {
- return MagpieParser.create(readSource(), mGrammar);
- }
-
- public void defineSyntax(String keyword, InfixParser parser, boolean export) {
- mGrammar.defineParser(keyword, parser);
-
- if (export) mExportedNames.add(keyword);
- }
-
- public void defineSyntax(String keyword, PrefixParser parser, boolean export) {
- mGrammar.defineParser(keyword, parser);
-
- if (export) mExportedNames.add(keyword);
- }
-
// Context inferface:
public Module getModule() {
@@ -163,6 +142,5 @@ public String toString() {
private final SourceFile mInfo;
private final Interpreter mInterpreter;
private final Scope mScope;
- private final Grammar mGrammar = new Grammar();
private final Set<String> mExportedNames = new HashSet<String>();
}
@@ -3,8 +3,6 @@
import java.util.*;
import java.util.Map.Entry;
-import com.stuffwithstuff.magpie.parser.InfixParser;
-import com.stuffwithstuff.magpie.parser.PrefixParser;
import com.stuffwithstuff.magpie.util.Expect;
import com.stuffwithstuff.magpie.util.Pair;
@@ -78,17 +76,6 @@ public void importName(String name, String rename, Module module,
throw new RuntimeException("Untested");
}
- // Import syntax.
- PrefixParser prefix = module.getGrammar().getPrefixParser(name);
- if (prefix != null) {
- mModule.defineSyntax(rename, prefix, export);
- }
-
- InfixParser infix = module.getGrammar().getInfixParser(name);
- if (infix != null) {
- mModule.defineSyntax(rename, infix, export);
- }
-
// Re-export.
if (export && (mParent == null)) {
mModule.export(name);
@@ -100,7 +100,7 @@ private static void registerMethod(Scope scope,
try {
// Process the annotation to get the method's Magpie name and type
// signature.
- MagpieParser parser = MagpieParser.create(text);
+ MagpieParser parser = new MagpieParser(text);
return parser.parseSignature();
} catch (ParseException e) {
// TODO(bob): Hack. Better error handling.
@@ -11,7 +11,6 @@
import com.stuffwithstuff.magpie.interpreter.Callable;
import com.stuffwithstuff.magpie.interpreter.Context;
import com.stuffwithstuff.magpie.interpreter.FnObj;
-import com.stuffwithstuff.magpie.interpreter.Interpreter;
import com.stuffwithstuff.magpie.interpreter.Name;
import com.stuffwithstuff.magpie.interpreter.Obj;
import com.stuffwithstuff.magpie.interpreter.PatternTester;
@@ -54,20 +53,17 @@ public Obj invoke(Context context, Obj left, Obj right) {
// TODO(bob): More or less temporary.
@Def("canParse(source is String)")
- public static class CheckSyntax implements Intrinsic {
+ public static class CanParse implements Intrinsic {
public Obj invoke(Context context, Obj left, Obj right) {
String source = right.asString();
boolean canParse = true;
try {
- Interpreter tempInterpreter = new Interpreter(context.getInterpreter().getHost());
- MagpieParser parser = MagpieParser.create(
- new StringReader("", source),
- tempInterpreter.getBaseModule().getGrammar());
+ MagpieParser parser = new MagpieParser(new StringReader("", source));
while (true) {
- Expr expr = parser.parseTopLevelExpression();
+ Expr expr = parser.parseStatement();
if (expr == null) break;
}
} catch (ParseException e) {
@@ -1,5 +1,6 @@
package com.stuffwithstuff.magpie.intrinsic;
+/*
import java.util.List;
import com.stuffwithstuff.magpie.Def;
@@ -15,8 +16,15 @@
import com.stuffwithstuff.magpie.parser.Token;
import com.stuffwithstuff.magpie.parser.TokenType;
import com.stuffwithstuff.magpie.util.Pair;
+*/
+
+// TODO(bob): Now that Magpie's grammar isn't extensible, this stuff isn't
+// being used. Keeping it around though because it may still be useful as a
+// module for playing with Magpie code in Magpie. When Magpie is self-hosted,
+// this can go away entirely.
public class SyntaxMethods {
+ /*
@Def("definePrefix(keyword is String, parser)")
public static class DefinePrefix implements Intrinsic {
public Obj invoke(Context context, Obj left, Obj right) {
@@ -177,4 +185,5 @@ public int getPrecedence() {
private final int mPrecedence;
private final Obj mParser;
}
+ */
}
@@ -14,34 +14,29 @@
import com.stuffwithstuff.magpie.util.Pair;
public class MagpieParser extends Parser {
- public static MagpieParser create(String text) {
- return create(new StringReader("", text));
+ public MagpieParser(String text) {
+ this(new StringReader("", text));
}
- public static MagpieParser create(SourceReader reader) {
- return create(reader, new Grammar());
+ public MagpieParser(SourceReader reader) {
+ this(new Annotator(new Morpher(new Lexer(reader))));
}
- public static MagpieParser create(SourceReader reader, Grammar grammar) {
- TokenReader lexer = new Lexer(reader);
- TokenReader morpher = new Morpher(lexer);
- TokenReader annotator = new Annotator(morpher);
-
- return new MagpieParser(annotator, grammar);
- }
-
- public MagpieParser(TokenReader tokens, Grammar grammar) {
+ public MagpieParser(TokenReader tokens) {
super(tokens);
-
- mGrammar = grammar;
+ mGrammar = new Grammar();
}
- public Expr parseTopLevelExpression() {
- if (lookAhead(TokenType.EOF)) return null;
+ public List<Expr> parseModule() {
+ List<Expr> exprs = new ArrayList<Expr>();
- Expr expr = parseStatement();
- if (!lookAhead(TokenType.EOF)) consume(TokenType.LINE);
- return expr;
+ while (!lookAhead(TokenType.EOF)) {
+ Expr expr = parseStatement();
+ exprs.add(expr);
+ if (!lookAhead(TokenType.EOF)) consume(TokenType.LINE);
+ }
+
+ return exprs;
}
/**
@@ -69,11 +64,11 @@ public Expr parseStatement() {
return parseExpression();
}
- public Expr parseExpression() {
+ Expr parseExpression() {
return parsePrecedence(0);
}
- public Expr parsePrecedence(int precedence) {
+ Expr parsePrecedence(int precedence) {
// Top down operator precedence parser based on:
// http://javascript.crockford.com/tdop/tdop.html
Token token = consume();
@@ -100,22 +95,22 @@ private Expr parseInfix(Expr left, int precedence) {
return left;
}
- public Expr parseBlock() {
+ Expr parseBlock() {
return parseBlock(true, new String[] { "end" }).getKey();
}
- public Expr parseExpressionOrBlock() {
+ Expr parseExpressionOrBlock() {
return parseExpressionOrBlock("end").getKey();
}
- public Pair<Expr, Token> parseExpressionOrBlock(String... endTokens) {
+ private Pair<Expr, Token> parseExpressionOrBlock(String... endTokens) {
return parseExpressionOrBlock(true, endTokens);
}
/**
* Parses a function type declaration.
*/
- public Pattern parseFunctionType() {
+ Pattern parseFunctionType() {
// Parse the prototype: (foo Foo, bar Bar)
consume(TokenType.LEFT_PAREN);
@@ -211,7 +206,7 @@ public Pattern parseFunctionType() {
return new Pair<String, Pattern>(name, pattern);
}
- public Expr groupExpression(TokenType right) {
+ Expr groupExpression(TokenType right) {
PositionSpan span = span();
if (match(right)) {
return Expr.nothing(span.end());

0 comments on commit 9de20ae

Please sign in to comment.