1 ;;; semantic-ast.el --- Simple Abstract Syntax Trees
3 ;; Copyright (C) 2003 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 25 June 2003
9 ;; X-RCS: $Id: semantic-ast.el,v 1.1 2007-11-26 15:10:32 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This is a simple implementation of Abstract Syntax Trees based on
31 ;; property lists. Each AST node is defined by a property and its
39 (defsubst semantic-ast-get (ast node)
40 "From the abstract syntax tree AST, return NODE value.
41 A node value is always a list or nil."
44 (defsubst semantic-ast-get1 (ast node)
45 "From the abstract syntax tree AST, return NODE first value.
46 A node value is always a list or nil."
47 (car (semantic-ast-get ast node)))
49 (defsubst semantic-ast-get-string (ast node)
50 "From the abstract syntax tree AST, return NODE value as a string.
51 Return concatenation of the strings in NODE value separated by a
53 (mapconcat #'(lambda (o) (format "%s" o))
54 (semantic-ast-get ast node)
57 (defun semantic-ast-add (ast &rest nodes)
58 "Update the abstract syntax tree AST with NODES.
59 NODES must be a sequence of NODE VALUE ..., where NODE is a symbol
60 that identifies the node, and VALUE is a Lisp object.
61 Add to the current value of NODE the associated new VALUE.
62 A node value is always a list or nil.
63 Return the updated abstract syntax tree."
66 (setq node (car nodes)
70 ast (plist-put ast node
71 (cons value (plist-get ast node)))))
74 (defun semantic-ast-put (ast &rest nodes)
75 "Update the abstract syntax tree AST with NODES.
76 NODES must be a sequence of NODE VALUE ..., where NODE is a symbol
77 that identifies a node, and VALUE is a Lisp object.
78 Replace the current value of each NODE with the associated new VALUE.
79 A node value is always a list or nil.
80 Return the updated abstract syntax tree."
83 (setq node (car nodes)
87 ast (plist-put ast node (list value))))
90 (defun semantic-ast-merge (ast1 ast2)
91 "Merge the abstract syntax trees AST1 and AST2.
92 Return the new merged abstract syntax tree."
93 (let ((ast (copy-sequence ast1))
98 ast (plist-put ast node
99 (append (plist-get ast node) (car ast2)))
103 (provide 'semantic-ast)
105 ;;; semantic-ast.el ends here