Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / semantic / semantic-ast.el
1 ;;; semantic-ast.el --- Simple Abstract Syntax Trees
2
3 ;; Copyright (C) 2003 David Ponce
4
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 25 June 2003
8 ;; Keywords: syntax
9 ;; X-RCS: $Id: semantic-ast.el,v 1.1 2007-11-26 15:10:32 michaels Exp $
10
11 ;; This file is not part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29 ;;
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
32 ;; value.
33 ;;
34
35 ;;; History:
36 ;;
37
38 ;;; Code:
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."
42   (plist-get ast node))
43
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)))
48
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
52 space."
53   (mapconcat #'(lambda (o) (format "%s" o))
54              (semantic-ast-get ast node)
55              " "))
56
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."
64   (let (node value)
65     (while nodes
66       (setq node  (car nodes)
67             nodes (cdr nodes)
68             value (car nodes)
69             nodes (cdr nodes)
70             ast (plist-put ast node
71                            (cons value (plist-get ast node)))))
72     ast))
73
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."
81   (let (node value)
82     (while nodes
83       (setq node  (car nodes)
84             nodes (cdr nodes)
85             value (car nodes)
86             nodes (cdr nodes)
87             ast (plist-put ast node (list value))))
88     ast))
89
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))
94         node)
95     (while ast2
96       (setq node (car ast2)
97             ast2 (cdr ast2)
98             ast  (plist-put ast node
99                             (append (plist-get ast node) (car ast2)))
100             ast2 (cdr ast2)))
101     ast))
102
103 (provide 'semantic-ast)
104
105 ;;; semantic-ast.el ends here