Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / parser / ParserState.java @ 7

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.parser;
29

    
30
import java.io.FileInputStream;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.util.ArrayList;
34
import java.util.List;
35

    
36
import org.antlr.runtime.ANTLRInputStream;
37
import org.antlr.runtime.ANTLRStringStream;
38
import org.antlr.runtime.CommonTokenStream;
39
import org.antlr.runtime.Token;
40
import org.momut.ooas.ast.IScope;
41
import org.momut.ooas.ast.identifiers.Identifier;
42
import org.momut.ooas.ast.identifiers.MainModule;
43
import org.momut.ooas.ast.identifiers.MethodIdentifier;
44
import org.momut.ooas.ast.types.OoActionSystemType;
45
import org.momut.ooas.ast.types.OpaqueType;
46
import org.momut.ooas.utils.exceptions.InternalCompilerException;
47

    
48
public class ParserState
49
{
50
        public ooaParser parser;
51
        public ooaLexer lexer;
52
        public String filename;
53
        public String namesTypePrefix = "";
54
        public boolean parsingAttributes;
55
        public List<String> mappingInformation = new ArrayList<>();
56

    
57

    
58
        public MainModule ooaSystem;
59

    
60
        public IScope currentScope;
61
        public ArrayList<ParserError> listOfParserErrors;
62
        public ArrayList<ParserWarning> listOfParserWarnings;
63
        public ArrayList<ParserMessage> listOfParserMessages;
64
        public ArrayList<OpaqueType> typesToFixUp;
65

    
66
        public ParserState(String unitName, InputStream toParse, String namedTypePrefix) throws IOException{
67
                try {
68
                        listOfParserErrors = new ArrayList<ParserError>();
69
                        listOfParserWarnings = new ArrayList<ParserWarning>();
70
                        listOfParserMessages = new ArrayList<ParserMessage>();
71
                        typesToFixUp = new ArrayList<OpaqueType>();
72
                        this.filename = unitName;
73
                        this.namesTypePrefix = namedTypePrefix;
74

    
75
                        final ANTLRStringStream input = new ANTLRInputStream(toParse); //, Encoding.ASCII); FIXME: Encoding.ASCII
76
                        lexer = new ooaLexer(input);
77
                        final CommonTokenStream tokens = new CommonTokenStream(lexer);
78
                        parser = new ooaParser(tokens);
79
                        parser.pState = this;
80
                } finally {
81
                        toParse.close();
82
                }
83
        }
84

    
85
        public ParserState(String fileToParse, String namedTypePrefix) throws IOException
86
        {
87
                this(fileToParse, new FileInputStream(fileToParse), namedTypePrefix);
88
        }
89

    
90

    
91
        public void AddErrorMessage(ParserError parserError)
92
        {
93
                listOfParserErrors.add(parserError);
94
        }
95
        public void AddWarningMessage(ParserWarning parserWarning)
96
        {
97
                listOfParserWarnings.add(parserWarning);
98
        }
99
        public void AddMessage(ParserMessage aMessage)
100
        {
101
                listOfParserMessages.add(aMessage);
102
        }
103

    
104
        public Identifier Lookup(String symbolName, IScope resolveStack)
105
        {
106
                final String lkupname = symbolName;
107
                do
108
                {
109
                        final Identifier result = resolveStack.ResolveIdentifier(lkupname);
110
                        if (result != null)
111
                                return result;
112
                        resolveStack = resolveStack.GetParentScope();
113
                } while (resolveStack != null);
114

    
115
                return null;
116
        }
117
        public Identifier Lookup(String symbolName)
118
        {
119
                return Lookup(symbolName, currentScope);
120
        }
121
        public Identifier Lookup(Token symbolName)
122
        {
123
                if (symbolName != null)
124
                        return Lookup(symbolName.getText());
125
                else
126
                        return null;
127
        }
128

    
129
        public void PushResolveStack(IScope newScope)
130
        {
131
                // some sanity check.
132
                if (newScope instanceof MethodIdentifier && !(currentScope instanceof OoActionSystemType))
133
                        throw new InternalCompilerException("Assertion violated: current scope no action system.");
134

    
135
                newScope.SetParentScope(currentScope);
136
                currentScope = newScope;
137
        }
138
        public void PopResolveStack()
139
        {
140
                currentScope = currentScope.GetParentScope();
141
        }
142
}