context.lisp 4.55 KB
Newer Older
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
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               context.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;    
;;;;    C11 compilation context.
;;;;    
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;;    2015-07-12 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;;    AGPL3
;;;;    
;;;;    Copyright Pascal J. Bourguignon 2015 - 2015
;;;;    
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
;;;;    
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;;    GNU Affero General Public License for more details.
;;;;    
;;;;    You should have received a copy of the GNU Affero General Public License
;;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
34
(in-package "COM.INFORMATIMAGO.LANGUAGES.C11.CONTEXT")
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

(defclass context ()
  ((c-identifiers-package :initarg :c-identifiers-package
                          :initform (load-time-value (find-package "COM.INFORMATIMAGO.LANGUAGES.C11.C"))
                          :accessor context-c-identifiers-package)
   (typedefs              :initarg :typedefs
                          :initform (make-hash-table :test (function equal))
                          :documentation "Maps identifier symbols to typedef declarations.
The scanner uses it to detect typedef_name tokens."
                          :reader context-typedefs)
   (functions             :initarg :functions
                          :initform (make-hash-table :test (function equal))
                          :documentation "Maps identifier symbols to function declarations.
The scanner uses it to detect func_name tokens."
                          :reader context-functions)
   (enumeration-constants :initarg :enum-constants
                          :initform (make-hash-table :test (function equal))
                          :documentation "Maps identifier symbols to enumeration constant declarations.
The scanner uses it to detect enumeration_constant tokens."
54 55 56
                          :reader context-enumeration-constants)
   (declaration-specifiers :initform '()
                           :accessor context-declaration-specifiers)))
57 58 59 60 61 62

(defvar *context* nil
  "The C11 parser context.")

(defgeneric typedef-name-p (context token)
  (:method ((context null) token)
63
    (declare (ignorable context token))
64 65 66 67
    nil))

(defgeneric function-name-p (context token)
  (:method ((context null) token)
68
    (declare (ignorable context token))
69 70 71 72
    nil))

(defgeneric enumeration-constant-name-p (context token)
  (:method ((context null) token)
73
    (declare (ignorable context token))
74 75 76
    nil))

(defun identifier-in-table-p (context table name)
77
  (declare (ignore context))
78
  (and (eq 'identifier (token-kind name))
79 80 81
       (gethash (token-symbol name) table)))

(defun enter-into-table (context table kind name definition)
82
  (declare (ignore context kind))
83
  (assert (eq 'identifier (token-kind name)) (name))
84 85 86 87 88 89 90 91 92 93 94 95 96
  (setf (gethash (token-symbol name) table) definition))

(defmethod typedef-name-p ((context context) token)
  (identifier-in-table-p context (context-typedefs context) token))

(defmethod function-name-p ((context context) token)
  (identifier-in-table-p context (context-functions context) token))

(defmethod enumeration-constant-name-p ((context context) token)
  (identifier-in-table-p context (context-enumeration-constants context) token))

(defgeneric enter-typedef (context name &optional definition)
  (:method ((context context) name &optional (definition t))
97
    (enter-into-table context (context-typedefs context) 'typedef-name name definition)))
98 99 100

(defgeneric enter-function (context name &optional definition)
  (:method ((context context) name &optional (definition t))
101
    (enter-into-table context (context-functions context) 'func-name name definition)))
102 103 104

(defgeneric enter-enumeration-constant (context name &optional definition)
  (:method ((context context) name &optional (definition t))
105
    (enter-into-table context (context-enumeration-constants context) 'enum-name name definition)))
106 107

;;;; THE END ;;;;
108