Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c68c6e137bafe30e91cca88b98b61f059f5d43c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * Copyright (c) 2008 INRIA.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     INRIA - initial API and implementation
 *
 * $Id: ParserLauncher.java,v 1.3 2008/06/28 17:12:25 fjouault Exp $
 */
package org.eclipse.gmt.tcs.injector;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.gmt.tcs.injector.TCSRuntime.RefSetting;
import org.eclipse.gmt.tcs.injector.wrappers.ParserWrapper;

/**
 * 
 * @author Mikaël Barbero
 * @author Frédéric Jouault
 *
 */
public class ParserLauncher {

	private final static int DEFAULT_TAB_SIZE = 8;
	
	private List refSettings;
	private ParserWrapper parserWrapper;

	public Object parse(ModelAdapter target, InputStream in, Map arguments) {
		Object ret = null;

		refSettings = new ArrayList();
		
		int tabSize = arguments.get("tabSize") == null ? DEFAULT_TAB_SIZE : Integer.parseInt((String)arguments.get("tabSize"));
		
		String parserGeneratorClassName = (String)arguments.get("parserGenerator");
		if(parserGeneratorClassName != null) {
			try {
				parserWrapper = (ParserWrapper)Class.forName("org.eclipse.gmt.tcs.injector.wrappers." + parserGeneratorClassName + ".ParserWrapper", true, Thread.currentThread().getContextClassLoader()).newInstance();
			} catch (InstantiationException e) {
				throw new IllegalArgumentException("Unable to instanciate parser generator with name " + parserGeneratorClassName, e);
			} catch (IllegalAccessException e) {
				throw new IllegalArgumentException("Unable to access to parser generator with name " + parserGeneratorClassName, e);
			} catch (ClassNotFoundException e) {
				throw new IllegalArgumentException("Unable to locate parser generator with name " + parserGeneratorClassName, e);
			}
		} else {
			// default parser generator
			parserWrapper = new org.eclipse.gmt.tcs.injector.wrappers.antlr3.ParserWrapper();
		}
		
		String nameAndProductionRule = (String)arguments.get("name");
		String ss[] = nameAndProductionRule.split("-");
		String name = ss[0];

		String productionRule = "main";
		if(ss.length > 1) {
			productionRule = ss[1];
		}

		TCSRuntime runtime = new TCSRuntime(target, parserWrapper, refSettings, arguments);
		parserWrapper.setRuntime(runtime);

		try {
			ret = parserWrapper.parse(tabSize, name, productionRule, in, arguments);
		} catch (Exception e) {
			e.printStackTrace();
		}

//		if(runtime.getNbErrors() == 0) {
			// A RefSetting that can autoCreate an element should come before other
			// RefSettings that might refer to this element.
			Collections.sort(refSettings, new Comparator() {
				public int compare(Object o1, Object o2) {
					RefSetting rs1 = (RefSetting)o1;
					RefSetting rs2 = (RefSetting)o2;
					if(rs1.canAutoCreate() && !rs2.canAutoCreate())
						return -1;	// autoCreate first
					else if(rs2.canAutoCreate() && !rs1.canAutoCreate())
						return 1;	// autoCreate first
					else			// both or none are autoCreate
						return 0;	// don't care
				}
			});

			// A RefSetting that relies on imported contexts can only be executed after
			// the RefSetting importing the corresponding context (and this, recursively).
			// For now, we sort RefSettings so that all those that import a context
			// are executed first.
			// If a RefSetting that imports a context relies on imported contexts,
			// then this solution is not enough.
			// The concept of imported context should be defined cleanly first.
			Collections.sort(refSettings, new Comparator() {
				public int compare(Object o1, Object o2) {
					RefSetting rs1 = (RefSetting)o1;
					RefSetting rs2 = (RefSetting)o2;
					if(rs1.importContext && !rs2.importContext)
						return -1;	// importContext first
					else if(rs2.importContext && !rs1.importContext)
						return 1;	// importContext first
					else			// both or none are importContext
						return 0;	// don't care
				}
			});
			
			for(Iterator i = refSettings.iterator() ; i.hasNext() ; ) {
				RefSetting rs = (RefSetting)i.next();
				try {
					rs.doIt();
				} catch(Exception e) {
					System.out.println("Warning: one refSetting crashed:");
					e.printStackTrace(System.out);
				}
			}
//		}

		return ret;
	}
}

Back to the top