PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/grammar/NATAbstractGrammar.java

http://ambienttalk.googlecode.com/
Java | 88 lines | 34 code | 10 blank | 44 comment | 3 complexity | 1c9cfbdbe03de5024dba292072d97d3a MD5 | raw file
 1/**
 2 * AmbientTalk/2 Project
 3 * NATAbstractGrammar.java created on 26-jul-2006 at 11:57:00
 4 * (c) Programming Technology Lab, 2006 - 2007
 5 * Authors: Tom Van Cutsem & Stijn Mostinckx
 6 * 
 7 * Permission is hereby granted, free of charge, to any person
 8 * obtaining a copy of this software and associated documentation
 9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28package edu.vub.at.objects.natives.grammar;
29
30import edu.vub.at.exceptions.InterpreterException;
31import edu.vub.at.exceptions.XSerializationError;
32import edu.vub.at.objects.ATAbstractGrammar;
33import edu.vub.at.objects.ATObject;
34import edu.vub.at.objects.ATTable;
35import edu.vub.at.objects.coercion.NativeTypeTags;
36import edu.vub.at.objects.natives.NATByCopy;
37import edu.vub.at.objects.natives.NATTable;
38import edu.vub.at.objects.natives.NATText;
39import edu.vub.at.parser.SourceLocation;
40import edu.vub.util.TempFieldGenerator;
41
42/**
43 * @author tvcutsem
44 *
45 * NATAbstractGrammar is the common superclass of all native ambienttalk objects
46 * that represent abstract grammar parse tree elements. That is, any object that
47 * can be returned as part of the AST produced by the native parser.
48 */
49public abstract class NATAbstractGrammar extends NATByCopy implements ATAbstractGrammar {
50	
51	// subclasses of NATAbstractGrammar will override meta_eval and meta_quote as appropriate,
52	// except for the literal grammar elements which can inherit the self-evaluating behaviour of NATNil.
53	
54    public ATTable meta_typeTags() throws InterpreterException {
55    	return NATTable.of(NativeTypeTags._ABSTRACTGRAMMAR_, NativeTypeTags._ISOLATE_);
56    }
57    
58    // AST nodes, like AmbientTalk objects, should have an assignable location,
59    // as long as the AST nodes are unique
60    
61    private SourceLocation loc_;
62    public SourceLocation impl_getLocation() { return loc_; }
63    public void impl_setLocation(SourceLocation loc) {
64    	// overriding the source location of an AG element
65    	// is probably the sign of a bug: locations should be single-assignment
66    	// to prevent mutable shared-state. That is, loc_ is effectively 'final'
67    	if (loc_ == null) {
68        	loc_ = loc;
69    	} else {
70    		throw new RuntimeException("Trying to override source location of "+this.toString()+" from "+loc_+" to "+loc);
71    	}
72    }
73    
74    // "normal" serialization results in the quoted value
75    // for unquoted values use impl_asUnquotedCode
76    public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
77    	NATText code = this.impl_asUnquotedCode(objectMap);
78    	return NATText.atValue("`" + code.javaValue);
79    }
80    
81    // should be overridden in the subclasses
82    public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
83    	throw new XSerializationError("Unable to serialize object: " + this.meta_print().javaValue + " (type: " + this.getClass() + ")");
84    }
85    
86    public boolean isNativeAbstractGrammar() { return true; }
87    
88}