PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/ooc/frontend/parser/VariableDeclParser.java

http://github.com/nddrylliog/ooc
Java | 143 lines | 125 code | 18 blank | 0 comment | 48 complexity | 95e2656cc9970654dc81d504b6af63dd MD5 | raw file
  1package org.ooc.frontend.parser;
  2
  3import org.ooc.frontend.model.Expression;
  4import org.ooc.frontend.model.Module;
  5import org.ooc.frontend.model.Node;
  6import org.ooc.frontend.model.NodeList;
  7import org.ooc.frontend.model.Type;
  8import org.ooc.frontend.model.VariableDecl;
  9import org.ooc.frontend.model.tokens.Token;
 10import org.ooc.frontend.model.tokens.TokenReader;
 11import org.ooc.frontend.model.tokens.Token.TokenType;
 12import org.ubi.CompilationFailedError;
 13import org.ubi.SourceReader;
 14
 15public class VariableDeclParser {
 16
 17	public static Node parseMulti(Module module, SourceReader sReader, TokenReader reader) {
 18		int mark = reader.mark();
 19		
 20		Token tName = reader.peek();
 21		if(!tName.isNameToken()) {
 22			reader.reset(mark);
 23			return null;
 24		}
 25		
 26		Token declStartToken = reader.peek();
 27		NodeList<VariableDecl> decls = new NodeList<VariableDecl>(declStartToken);
 28		Token atomStartToken;
 29		while((atomStartToken = reader.peek()).isNameToken()) {
 30			String name = reader.read().get(sReader);
 31			Expression expr = null;
 32			if(reader.peek().type == TokenType.ASSIGN) {
 33				reader.skip();
 34				expr = ExpressionParser.parse(module, sReader, reader, true);
 35				if(expr == null) {
 36					throw new CompilationFailedError(sReader.getLocation(reader.prev()),
 37							"Expected expression as an initializer to a variable declaration.");
 38				}
 39			} else if(reader.peek().type == TokenType.DECL_ASSIGN) {
 40				reader.skip();
 41				
 42				boolean isConst = false, isStatic = false;
 43				while(true) {
 44					Token kw = reader.peek();
 45					if(kw.type == TokenType.CONST_KW) {
 46						reader.skip();
 47						isConst = true;
 48					} else if(kw.type == TokenType.STATIC_KW) {
 49						reader.skip();
 50						isStatic = true;
 51					} else break;
 52				}
 53				
 54				expr = ExpressionParser.parse(module, sReader, reader);
 55				if(expr == null) {
 56					throw new CompilationFailedError(sReader.getLocation(reader.prev()),
 57							"Expected expression as an initializer to a variable declaration.");
 58				}
 59				VariableDecl vdfe = new VariableDecl(null, name, expr, atomStartToken, module);
 60				vdfe.setConst(isConst);
 61				vdfe.setStatic(isStatic);
 62				return vdfe;
 63			}
 64			VariableDecl vd = new VariableDecl(null, name, expr, atomStartToken, module);
 65			decls.add(vd);
 66			if(reader.peek().type != TokenType.COMMA) break;
 67			reader.skip();
 68			reader.skipWhitespace();
 69		}
 70		
 71		if(reader.read().type != TokenType.COLON) {
 72			reader.reset(mark);
 73			return null;
 74		}
 75		
 76		boolean isStatic = false;
 77		boolean isProto = false;
 78		String externName = null;
 79		String unmangledName = null;
 80		
 81		while(true) {
 82			Token t = reader.peek();
 83			if(t.type == TokenType.STATIC_KW) {
 84				isStatic = true;
 85				reader.skip();
 86			} else if(t.type == TokenType.PROTO_KW) {
 87				isProto = true;
 88				reader.skip();
 89			} else if(t.type == TokenType.EXTERN_KW) {
 90				externName = ExternParser.parse(sReader, reader);
 91			} else if(t.type == TokenType.UNMANGLED_KW) {
 92				unmangledName = UnmangledParser.parse(sReader, reader);
 93			} else {
 94				break;
 95			}
 96		}
 97		
 98		Type type = TypeParser.parse(module, sReader, reader);
 99		if(type == null) {
100			reader.reset(mark);
101			return null;
102		}
103		if(decls.size() == 1 && decls.getFirst().getExpression() == null) {
104			if(reader.peek().type == TokenType.ASSIGN) {
105				reader.skip();
106				Expression expr = ExpressionParser.parse(module, sReader, reader);
107				if(expr == null) {
108					throw new CompilationFailedError(sReader.getLocation(reader.prev()),
109							"Expected expression as an initializer to a variable declaration.");
110				}
111				decls.getFirst().setExpression(expr);
112			}
113		}
114		
115		for(VariableDecl decl : decls) {
116			decl.setType(type);
117			decl.setStatic(isStatic);
118			decl.setProto(isProto);
119			decl.setExternName(externName);
120			decl.setUnmangledName(unmangledName);
121		}
122		
123		if(decls.size() == 1) {
124			return decls.getFirst();
125		}
126		return decls;
127	}
128
129	public static VariableDecl parseSingle(Module module, SourceReader sReader,
130			TokenReader reader) {
131		
132		int mark = reader.mark();
133		
134		Node node = parseMulti(module, sReader, reader);
135		if(node == null || !(node instanceof VariableDecl)) {
136			reader.reset(mark);
137			return null;
138		}
139		return (VariableDecl) node;
140		
141	}
142
143}