Changeset 12706


Ignore:
Timestamp:
May 2, 2019 2:41:44 PM (8 months ago)
Author:
tgutzmann
Message:

#5764 resolve scanner/parser at runtime, not compile time, to support Optimica-builds.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/QualifiedName.java

    r12700 r12706  
    1717
    1818import java.io.IOException;
     19import java.io.Reader;
    1920import java.io.StringReader;
     21import java.lang.reflect.Constructor;
    2022import java.util.ArrayList;
    2123import java.util.Iterator;
    2224
    23 import org.jmodelica.modelica.parser.ModelicaParser.Terminals;
    24 import org.jmodelica.modelica.parser.ModelicaScanner;
    2525import org.jmodelica.util.exceptions.NameFormatException;
    2626
     27import beaver.Scanner;
    2728import beaver.Scanner.Exception;
    2829import beaver.Symbol;
     
    3233 */
    3334public class QualifiedName {
     35    private static Constructor<? extends Scanner> scannerConstructor;
     36    private static int ID_TOKEN_VALUE;
     37    private static int EOF_TOKEN_VALUE;
     38    private static int DOT_TOKEN_VALUE;
     39   
     40    @SuppressWarnings("unchecked")
     41    private static void initInternal() {
     42        try {
     43            scannerConstructor = ((Class<? extends Scanner>)Class.forName("org.jmodelica.modelica.parser.ModelicaScanner")).getConstructor(Reader.class);
     44            Class<?> clazz = Class.forName("org.jmodelica.modelica.parser.ModelicaParser$Terminals");
     45            ID_TOKEN_VALUE = clazz.getField("ID").getInt(null);
     46            EOF_TOKEN_VALUE = clazz.getField("EOF").getInt(null);
     47            DOT_TOKEN_VALUE = clazz.getField("DOT").getInt(null);
     48        } catch (Throwable e) {
     49            try {
     50                scannerConstructor = ((Class<? extends Scanner>)Class.forName("org.jmodelica.optimica.parser.ModelicaScanner")).getConstructor(Reader.class);
     51                Class<?> clazz = Class.forName("org.jmodelica.optimica.parser.ModelicaParser$Terminals");
     52                ID_TOKEN_VALUE = clazz.getField("ID").getInt(null);
     53                EOF_TOKEN_VALUE = clazz.getField("EOF").getInt(null);
     54                DOT_TOKEN_VALUE = clazz.getField("DOT").getInt(null);
     55            } catch (Throwable e2) {
     56                // This should never happen; simply throw a RuntimeException.
     57                throw new RuntimeException("Scanner class not found");
     58            }
     59        }
     60    }
     61    static {
     62        initInternal();
     63    }
     64   
    3465    private final boolean isGlobal;
    3566    private final ArrayList<String> names = new ArrayList<>();
     
    6495    }
    6596
     97    private static Scanner newScanner(String name) {
     98        try {
     99            return scannerConstructor.newInstance(new StringReader(name));
     100        } catch (Throwable e) {
     101            throw new RuntimeException("Unhandled internal error", e);
     102        }
     103    }
     104   
    66105    /**
    67106     * Checks if the name is a valid and simple (unqualified) identifier.
     
    75114            name = name.substring(1, name.length());
    76115        }
    77         ModelicaScanner ms = new ModelicaScanner(new StringReader(name));
     116        Scanner ms = newScanner(name);
     117
    78118        try {
    79            if (ms.nextToken().getId() != Terminals.ID)
     119           if (ms.nextToken().getId() != ID_TOKEN_VALUE)
    80120               return false;
    81            if (ms.nextToken().getId() != Terminals.EOF)
     121           if (ms.nextToken().getId() != EOF_TOKEN_VALUE)
    82122               return false;
    83123           return true;
     
    112152            name = name.substring(start, end);
    113153        }
    114         ModelicaScanner ms = new ModelicaScanner(new StringReader(name));
     154        Scanner ms = newScanner(name);
    115155        try {
    116156            Symbol sym;
    117157            do {
    118158                sym = ms.nextToken();
    119                 if (sym.getId() != Terminals.ID)
     159                if (sym.getId() != ID_TOKEN_VALUE)
    120160                    throw new NameFormatException("The qualified name is not valid");
    121161                names.add((String)sym.value);
    122             } while ((sym = ms.nextToken()).getId() == Terminals.DOT);
    123             if (sym.getId() != Terminals.EOF)
     162            } while ((sym = ms.nextToken()).getId() == DOT_TOKEN_VALUE);
     163            if (sym.getId() != EOF_TOKEN_VALUE)
    124164                throw new NameFormatException("Invalid name: " + name);
    125165        } catch (IOException e) {
Note: See TracChangeset for help on using the changeset viewer.