Permalink
Browse files

initial import

  • Loading branch information...
1 parent 3f32eeb commit bb2e10500693e0da0edfc052d13da9fe0db11f23 @bendisposto committed Dec 13, 2011
Showing 543 changed files with 557,769 additions and 0 deletions.
View
@@ -0,0 +1,19 @@
+build/
+ParserAspects/
+bin/
+.classpath
+.project
+**/.settings/org.eclipse.jdt.core.prefs
+.gradle/
+logs/
+*.swp
+*.log
+*~
+! .gitkeep
+.DS_Store
+tmp/
+PROB_LOGFILE_IS_UNDEFINED
+**/.settings/org.eclipse.jdt.ui.prefs
+src/main/resources/build.properties
+bparser/src/main/resources/revision.properties
+config.groovy
View
@@ -0,0 +1,38 @@
+# ProB Parser Library
+
+## Building
+Run the 'deploy' target with gradle. If you don't have gradle installed, you can use the provided wrapper
+ ./gradlew deploy
+
+The artifacts are copied to the build folder
+
+## Content
+
+- bparser - parser for classical B (more information in http://www.stups.uni-duesseldorf.de/w/An_Object_Oriented_Parser_For_B_Specifications)
+- prologlib - library to construct and manipulate well-formed prolog terms
+- parserbase - library for uniform access to the formal language parsers (e.g. to embed a language into ltl)
+- ltlparser - parser for LTL formulas - the parser delegates formulas in { } to a formalism specific parser (e.g. to the classical B parser)
+- answerparser - parser to read answers from the ProB prolog core (deprecated!)
+- unicode - lexer that transforms Event-B expressions and predicates from ASCII to Unicode syntax and vice3 versa (note: this is not extensible!)
+- cliparser - glue code for embedding the parser in the prolog core (deprecated!)
+
+## Contributors
+The libraries contain contributions from (in alphabetical order)
+Jens Bendisposto, Marc Büngener, Fabian Fritz, Michael Leuschel; Daniel Plagge
+
+## Licence
+
+The ProB Parser Library source code is distributed under the Eclipse Public License - v 1.0 (see epl-v10.html)
+
+The Parser Library comes with ABSOLUTELY NO WARRANTY OF ANY KIND !
+This software is distributed in the hope that it will be useful
+but WITHOUT ANY WARRANTY. The author(s) do not accept responsibility
+to anyone for the consequences of using it or for whether it serves
+any particular purpose or works at all. No warranty is made about
+the software or its performance.
+
+
+## Commercial Support
+For availability of commercial support, please contact Formal Mind (http://www.formalmind.com/).
+
+(c) 2011 STUPS group, University of Düsseldorf
View
@@ -0,0 +1,64 @@
+// ProB Answer Parser
+
+dependsOn(':prologlib')
+
+
+
+dependencies {
+ sablecc(group: 'sablecc', name: 'sablecc', version: '3.2')
+ compile project(path: ":prologlib", configuration: "archives")
+}
+
+sourceSets {
+ main {
+ java {
+ srcDirs = ['build/temp','src/main/java']
+ }
+ }
+}
+
+task genParserTasks {
+ def generated = []
+ inputs.dir new File('src/main/resources')
+ outputs.dir new File('build/temp','generated')
+ fileTree {
+ from 'src/main/resources'
+ include '**/*.scc'
+ }.each {
+ name ->
+ def tn = name.getName().split("\\.")[0]
+ task "parser$tn"(type:JavaExec) {
+ doFirst{ file('build/temp').mkdirs() }
+ main = 'org.sablecc.sablecc.SableCC'
+ // classpath = sourceSets.main.compileClasspath
+ classpath = configurations.sablecc
+ maxHeapSize = '1024m'
+ args = ['-d','build/temp',name]
+ }
+ generated += "parser$tn"
+ }
+
+ doLast {
+ generated.each{
+ tasks.findByName(it).execute()
+ }
+ }
+}
+
+
+jar {
+ include '**/*.class'
+ exclude '**.*.scc'
+ from 'build/temp'
+ include '**/*.dat'
+}
+
+compileJava {
+ dependsOn = ['genParserTasks',':prologlib:deploy']
+}
+
+sourceSets.test.runtimeClasspath += files(sourceSets.main.java.srcDirs)
+
+
+
+
@@ -0,0 +1,250 @@
+/**
+ * (c) 2009 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, Heinrich
+ * Heine Universitaet Duesseldorf This software is licenced under EPL 1.0
+ * (http://www.eclipse.org/org/documents/epl-v10.html)
+ * */
+
+/**
+ *
+ */
+package de.prob.parser;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import de.prob.core.sablecc.node.Start;
+import de.prob.prolog.term.CompoundPrologTerm;
+import de.prob.prolog.term.IntegerPrologTerm;
+import de.prob.prolog.term.ListPrologTerm;
+import de.prob.prolog.term.PrologTerm;
+import de.prob.prolog.term.VariablePrologTerm;
+
+/**
+ * Takes a Prolog term of the form (only its canonical form) "[x=a,y=b,z=c]" and
+ * converts it into a mapping x->a, y->b, z->c, where x,y and z are identifiers
+ * and a,b,c prolog terms.
+ *
+ * @author plagge
+ */
+public class BindingGenerator {
+
+ public static Map<String, PrologTerm> createBinding(final Start ast)
+ throws ResultParserException {
+ PrologTerm term = PrologTermGenerator.toPrologTerm(ast);
+ return createBinding(term);
+ }
+
+ public static Map<String, PrologTerm> createBindingMustNotFail(
+ final String query, final Start ast) throws ResultParserException {
+ PrologTerm term = PrologTermGenerator.toPrologTermMustNotFail(query,
+ ast);
+ return createBinding(term);
+ }
+
+ private static Map<String, PrologTerm> createBinding(final PrologTerm term) {
+ Map<String, PrologTerm> result;
+ if (term == null) {
+ result = null;
+ } else if (term.isList()) {
+ ListPrologTerm list = (ListPrologTerm) term;
+ result = createBinding(list);
+ } else
+ throw new IllegalArgumentException("Expected list");
+ return result;
+ }
+
+ private static Map<String, PrologTerm> createBinding(
+ final ListPrologTerm list) {
+ Map<String, PrologTerm> result;
+ result = new HashMap<String, PrologTerm>();
+ for (int i = 0; i < list.size(); i++) {
+ PrologTerm element = list.get(i);
+ if (element.isTerm()) {
+ CompoundPrologTerm binding = (CompoundPrologTerm) element;
+ if (binding.getArity() == 2 && "=".equals(binding.getFunctor())) {
+ extractBinding(result, binding);
+ } else
+ throw new IllegalArgumentException(
+ "Expected binding (=/2), but was "
+ + binding.getFunctor() + "/"
+ + String.valueOf(binding.getArity()));
+ } else
+ throw new IllegalArgumentException(
+ "Expected binding but was not a term");
+ }
+ return Collections.unmodifiableMap(result);
+ }
+
+ private static void extractBinding(final Map<String, PrologTerm> result,
+ final CompoundPrologTerm binding) {
+ PrologTerm varterm = binding.getArgument(1);
+ if (varterm.isAtom()) {
+ String name = ((CompoundPrologTerm) varterm).getFunctor();
+ PrologTerm value = binding.getArgument(2);
+ result.put(name, value);
+ } else
+ throw new IllegalArgumentException(
+ "Expected atomic variable name, but found "
+ + varterm.toString());
+ }
+
+ public static CompoundPrologTerm getCompoundTerm(final PrologTerm term,
+ final String functor, final int arity) throws ResultParserException {
+ return checkSignature(checkComponentType(term), functor, arity);
+ }
+
+ private static CompoundPrologTerm checkComponentType(final PrologTerm term)
+ throws ResultParserException {
+ if (!(term instanceof CompoundPrologTerm)) {
+ final String message = "Expected CompoundPrologTerm, but got "
+ + term.getClass().getSimpleName();
+ throw new ResultParserException(message, null);
+ }
+ return (CompoundPrologTerm) term;
+ }
+
+ public static CompoundPrologTerm getCompoundTerm(final PrologTerm term,
+ final int arity) throws ResultParserException {
+ if ((term instanceof CompoundPrologTerm))
+ return checkArity((CompoundPrologTerm) term, arity);
+ final String message = "Expected CompoundPrologTerm, but got "
+ + term.getClass().getSimpleName();
+ throw new ResultParserException(message, null);
+ }
+
+ public static CompoundPrologTerm getCompoundTerm(
+ final Map<String, PrologTerm> bindings, final String name,
+ final String functor, final int arity) throws ResultParserException {
+ final PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getCompoundTerm(prologTerm, functor, arity);
+ }
+
+ public static CompoundPrologTerm getCompoundTerm(
+ final Map<String, PrologTerm> bindings, final String name,
+ final int arity) throws ResultParserException {
+ final PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getCompoundTerm(prologTerm, arity);
+ }
+
+ private static CompoundPrologTerm checkSignature(
+ final CompoundPrologTerm term, final String functor, final int arity)
+ throws ResultParserException {
+ checkArity(term, arity);
+ checkFunctor(term, functor);
+ return term;
+ }
+
+ private static CompoundPrologTerm checkFunctor(
+ final CompoundPrologTerm term, final String functor)
+ throws ResultParserException {
+ if (!term.getFunctor().equals(functor)) {
+ final String message = "Expected " + term + " to have functor "
+ + functor + ", but got " + term.getFunctor();
+ throw new ResultParserException(message, null);
+ }
+ return term;
+ }
+
+ private static CompoundPrologTerm checkArity(final CompoundPrologTerm term,
+ final int arity) throws ResultParserException {
+ if (term.getArity() != arity) {
+ final String message = "Expected " + term + " to have an arity "
+ + arity + ", but got " + term.getArity();
+ throw new ResultParserException(message, null);
+ }
+ return term;
+ }
+
+ public static ListPrologTerm getList(final PrologTerm term)
+ throws ResultParserException {
+ if (term instanceof ListPrologTerm)
+ return (ListPrologTerm) term;
+ final String message = "Expected ListPrologTerm, but got "
+ + term.getClass().getSimpleName();
+ throw new ResultParserException(message, null);
+ }
+
+ public static ListPrologTerm getList(
+ final Map<String, PrologTerm> bindings, final String name)
+ throws ResultParserException {
+ PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getList(prologTerm);
+ }
+
+ public static ListPrologTerm getList(
+ final ISimplifiedROMap<String, PrologTerm> bindings,
+ final String name) throws ResultParserException {
+ PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getList(prologTerm);
+ }
+
+ public static IntegerPrologTerm getInteger(final PrologTerm term)
+ throws ResultParserException {
+ if (term instanceof IntegerPrologTerm)
+ return (IntegerPrologTerm) term;
+ final String message = "Expected ListPrologTerm, but got "
+ + term.getClass().getSimpleName();
+ throw new ResultParserException(message, null);
+ }
+
+ public static IntegerPrologTerm getInteger(
+ final Map<String, PrologTerm> bindings, final String name)
+ throws ResultParserException {
+ PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getInteger(prologTerm);
+ }
+
+ public static VariablePrologTerm getVariable(final PrologTerm term)
+ throws ResultParserException {
+ if (term instanceof VariablePrologTerm)
+ return (VariablePrologTerm) term;
+ final String message = "Expected ListPrologTerm, but got "
+ + term.getClass().getSimpleName();
+ throw new ResultParserException(message, null);
+ }
+
+ public static VariablePrologTerm getVariable(
+ final Map<String, PrologTerm> bindings, final String name)
+ throws ResultParserException {
+ PrologTerm prologTerm = getFromBindings(bindings, name);
+ return getVariable(prologTerm);
+ }
+
+ private static PrologTerm getFromBindings(
+ final Map<String, PrologTerm> bindings, final String name)
+ throws ResultParserException {
+ PrologTerm prologTerm = bindings.get(name);
+ if (prologTerm == null) {
+ final String message = "Cannot extract " + name
+ + " from bindings.\n" + listBindings(bindings);
+ throw new ResultParserException(message, null);
+ }
+ return prologTerm;
+ }
+
+ private static PrologTerm getFromBindings(
+ final ISimplifiedROMap<String, PrologTerm> bindings,
+ final String name) throws ResultParserException {
+ PrologTerm prologTerm = bindings.get(name);
+ if (prologTerm == null) {
+ final String message = "Cannot extract " + name
+ + " from bindings.\n";
+ throw new ResultParserException(message, null);
+ }
+ return prologTerm;
+ }
+
+ private static String listBindings(final Map<String, PrologTerm> bindings) {
+ StringBuilder sb = new StringBuilder();
+ for (Entry<String, PrologTerm> e : bindings.entrySet()) {
+ sb.append(e.getKey());
+ sb.append(" -> ");
+ sb.append(e.getValue().toString());
+ sb.append('\n');
+ }
+ return sb.toString();
+ }
+
+}
@@ -0,0 +1,15 @@
+package de.prob.parser;
+
+/**
+ * This interface describes a completely stripped-down version of a read-only
+ * map. Its purpose is to allow it to implement easily a rewriting-strategy for
+ * keys without working out all those details in all the {@link Map}
+ *
+ * The class {@link SimplifiedROMap} provides a simple wrapper class to use a
+ * {@link Map} as a {@link ISimplifiedROMap}.
+ *
+ * @author plagge
+ */
+public interface ISimplifiedROMap<K, V> {
+ V get(K key);
+}
Oops, something went wrong.

0 comments on commit bb2e105

Please sign in to comment.