/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. */
  28. package edu.vub.at.objects.natives.grammar;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XSerializationError;
  31. import edu.vub.at.objects.ATAbstractGrammar;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.objects.coercion.NativeTypeTags;
  35. import edu.vub.at.objects.natives.NATByCopy;
  36. import edu.vub.at.objects.natives.NATTable;
  37. import edu.vub.at.objects.natives.NATText;
  38. import edu.vub.at.parser.SourceLocation;
  39. import edu.vub.util.TempFieldGenerator;
  40. /**
  41. * @author tvcutsem
  42. *
  43. * NATAbstractGrammar is the common superclass of all native ambienttalk objects
  44. * that represent abstract grammar parse tree elements. That is, any object that
  45. * can be returned as part of the AST produced by the native parser.
  46. */
  47. public abstract class NATAbstractGrammar extends NATByCopy implements ATAbstractGrammar {
  48. // subclasses of NATAbstractGrammar will override meta_eval and meta_quote as appropriate,
  49. // except for the literal grammar elements which can inherit the self-evaluating behaviour of NATNil.
  50. public ATTable meta_typeTags() throws InterpreterException {
  51. return NATTable.of(NativeTypeTags._ABSTRACTGRAMMAR_, NativeTypeTags._ISOLATE_);
  52. }
  53. // AST nodes, like AmbientTalk objects, should have an assignable location,
  54. // as long as the AST nodes are unique
  55. private SourceLocation loc_;
  56. public SourceLocation impl_getLocation() { return loc_; }
  57. public void impl_setLocation(SourceLocation loc) {
  58. // overriding the source location of an AG element
  59. // is probably the sign of a bug: locations should be single-assignment
  60. // to prevent mutable shared-state. That is, loc_ is effectively 'final'
  61. if (loc_ == null) {
  62. loc_ = loc;
  63. } else {
  64. throw new RuntimeException("Trying to override source location of "+this.toString()+" from "+loc_+" to "+loc);
  65. }
  66. }
  67. // "normal" serialization results in the quoted value
  68. // for unquoted values use impl_asUnquotedCode
  69. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  70. NATText code = this.impl_asUnquotedCode(objectMap);
  71. return NATText.atValue("`" + code.javaValue);
  72. }
  73. // should be overridden in the subclasses
  74. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  75. throw new XSerializationError("Unable to serialize object: " + this.meta_print().javaValue + " (type: " + this.getClass() + ")");
  76. }
  77. public boolean isNativeAbstractGrammar() { return true; }
  78. }