From 7f634373e0e0b3ca8fbd71b421591ce8be52605f Mon Sep 17 00:00:00 2001 From: Mark Walters Date: Sat, 27 Oct 2012 10:34:12 +0100 Subject: [PATCH] [PATCH v4 3/3] emacs: Move the incremental JSON parser to notmuch-lib.el --- 57/8d9fe5952afaf3c09dcbd84ca22fc0d95a6030 | 240 ++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 57/8d9fe5952afaf3c09dcbd84ca22fc0d95a6030 diff --git a/57/8d9fe5952afaf3c09dcbd84ca22fc0d95a6030 b/57/8d9fe5952afaf3c09dcbd84ca22fc0d95a6030 new file mode 100644 index 000000000..f0c252d8a --- /dev/null +++ b/57/8d9fe5952afaf3c09dcbd84ca22fc0d95a6030 @@ -0,0 +1,240 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 3CBCB431FAE + for ; Sat, 27 Oct 2012 02:34:30 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0.201 +X-Spam-Level: +X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, + RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id DIvgVJ84Q67x for ; + Sat, 27 Oct 2012 02:34:27 -0700 (PDT) +Received: from mail-wi0-f173.google.com (mail-wi0-f173.google.com + [209.85.212.173]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 379AA429E31 + for ; Sat, 27 Oct 2012 02:34:26 -0700 (PDT) +Received: by mail-wi0-f173.google.com with SMTP id hm4so822748wib.2 + for ; Sat, 27 Oct 2012 02:34:24 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; + bh=oniCK/ZY6Z1v/prdVAZ+7F+TvTh2rw/L1hNDN5I/gaM=; + b=mIk7P6gbCWlbIBVcmN+UQkAQzn0+jT87AVPHccw1G55NcQN3PSgWVNUf6LtBCzp4pH + /BFTb1V/X8d7Ceb15N1RF5MgLVVeShsH+xvyJOi613sKc8tMLkBxmLIhaFg6lFQLYT3r + fXSlon143KLv1e1eDicuwAfkKsOIiu+eYi2OEosF9p3dTHoTUQdFQtcXTwXU8ADIV8rH + p8xMTRF4JBuTMmfWhyBGvgIZmXyhLqF9jEoGMV5JbIAJWgwQrW4QRv5E6VzSKTqI0Fph + 2ABnBRb+rir7kh/4qpuf8w6q9dBqptMySEeecgFh6Mqmwr6GPYa2fN1Z9AsYmqvf+6YU + iaNQ== +Received: by 10.216.197.205 with SMTP id t55mr15020046wen.156.1351330464756; + Sat, 27 Oct 2012 02:34:24 -0700 (PDT) +Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) + by mx.google.com with ESMTPS id hv8sm1607198wib.0.2012.10.27.02.34.23 + (version=TLSv1/SSLv3 cipher=OTHER); + Sat, 27 Oct 2012 02:34:24 -0700 (PDT) +From: Mark Walters +To: notmuch@notmuchmail.org +Subject: [PATCH v4 3/3] emacs: Move the incremental JSON parser to + notmuch-lib.el +Date: Sat, 27 Oct 2012 10:34:12 +0100 +Message-Id: <1351330452-8348-4-git-send-email-markwalters1009@gmail.com> +X-Mailer: git-send-email 1.7.9.1 +In-Reply-To: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com> +References: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 27 Oct 2012 09:34:30 -0000 + +This just moves the newly split out incremental json parser (together +with its state variables) to lib. + +There should be no functional change. +--- + emacs/notmuch-lib.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ + emacs/notmuch.el | 63 ----------------------------------------------- + 2 files changed, 66 insertions(+), 63 deletions(-) + +diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el +index 69867ad..8b6115b 100644 +--- a/emacs/notmuch-lib.el ++++ b/emacs/notmuch-lib.el +@@ -341,6 +341,16 @@ was called." + + ;; Incremental JSON parsing + ++;; These two variables are internal variables to the parsing ++;; routines. They are always used buffer local but need to be declared ++;; globally to avoid compiler warnings. ++ ++(defvar notmuch-json-parser nil ++ "Internal incremental JSON parser object: local to the buffer being parsed.") ++ ++(defvar notmuch-json-state nil ++ "State of the internal JSON parser: local to the buffer being parsed.") ++ + (defun notmuch-json-create-parser (buffer) + "Return a streaming JSON parser that consumes input from BUFFER. + +@@ -535,6 +545,62 @@ of the buffer if there is only trailing whitespace." + (unless (eobp) + (signal 'json-error (list "Trailing garbage following JSON data"))))) + ++(defun notmuch-json-parse-partial-list (result-function error-function results-buf) ++ "Parse a partial JSON list from current buffer. ++ ++This function consumes a JSON list from the current buffer, ++applying RESULT-FUNCTION in buffer RESULT-BUFFER to each complete ++value in the list. It operates incrementally and should be ++called whenever the buffer has been extended with additional ++data. ++ ++If there is a syntax error, this will attempt to resynchronize ++with the input and will apply ERROR-FUNCTION in buffer ++RESULT-BUFFER to any input that was skipped. ++ ++It sets up all the needed internal variables: the caller just ++needs to call it with point in the same place that the parser ++left it." ++ (let (done) ++ (unless (local-variable-p 'notmuch-json-parser) ++ (set (make-local-variable 'notmuch-json-parser) ++ (notmuch-json-create-parser (current-buffer))) ++ (set (make-local-variable 'notmuch-json-state) 'begin)) ++ (while (not done) ++ (condition-case nil ++ (case notmuch-json-state ++ ((begin) ++ ;; Enter the results list ++ (if (eq (notmuch-json-begin-compound ++ notmuch-json-parser) 'retry) ++ (setq done t) ++ (setq notmuch-json-state 'result))) ++ ((result) ++ ;; Parse a result ++ (let ((result (notmuch-json-read notmuch-json-parser))) ++ (case result ++ ((retry) (setq done t)) ++ ((end) (setq notmuch-json-state 'end)) ++ (otherwise (with-current-buffer results-buf ++ (funcall result-function result)))))) ++ ((end) ++ ;; Any trailing data is unexpected ++ (notmuch-json-eof notmuch-json-parser) ++ (setq done t))) ++ (json-error ++ ;; Do our best to resynchronize and ensure forward ++ ;; progress ++ (let ((bad (buffer-substring (line-beginning-position) ++ (line-end-position)))) ++ (forward-line) ++ (with-current-buffer results-buf ++ (funcall error-function "%s" bad)))))) ++ ;; Clear out what we've parsed ++ (delete-region (point-min) (point)))) ++ ++ ++ ++ + (provide 'notmuch-lib) + + ;; Local Variables: +diff --git a/emacs/notmuch.el b/emacs/notmuch.el +index e3610d6..e304dde 100644 +--- a/emacs/notmuch.el ++++ b/emacs/notmuch.el +@@ -820,16 +820,6 @@ non-authors is found, assume that all of the authors match." + (insert (apply #'format string objects)) + (insert "\n"))) + +-;; These two variables are internal variables to the parsing +-;; routines. They are always used buffer local but need to be declared +-;; globally to avoid compiler warnings. +- +-(defvar notmuch-json-state nil +- "Internal incremental JSON parser object: local to the buffer being parsed.") +- +-(defvar notmuch-json-parser nil +- "State of the internal JSON parser: local to the buffer being parsed.") +- + (defun notmuch-search-process-filter (proc string) + "Process and filter the output of \"notmuch search\"" + (let ((results-buf (process-buffer proc)) +@@ -847,59 +837,6 @@ non-authors is found, assume that all of the authors match." + 'notmuch-search-show-error + results-buf))))) + +-(defun notmuch-json-parse-partial-list (result-function error-function results-buf) +- "Parse a partial JSON list from current buffer. +- +-This function consumes a JSON list from the current buffer, +-applying RESULT-FUNCTION in buffer RESULT-BUFFER to each complete +-value in the list. It operates incrementally and should be +-called whenever the buffer has been extended with additional +-data. +- +-If there is a syntax error, this will attempt to resynchronize +-with the input and will apply ERROR-FUNCTION in buffer +-RESULT-BUFFER to any input that was skipped. +- +-It sets up all the needed internal variables: the caller just +-needs to call it with point in the same place that the parser +-left it." +- (let (done) +- (unless (local-variable-p 'notmuch-json-parser) +- (set (make-local-variable 'notmuch-json-parser) +- (notmuch-json-create-parser (current-buffer))) +- (set (make-local-variable 'notmuch-json-state) 'begin)) +- (while (not done) +- (condition-case nil +- (case notmuch-json-state +- ((begin) +- ;; Enter the results list +- (if (eq (notmuch-json-begin-compound +- notmuch-json-parser) 'retry) +- (setq done t) +- (setq notmuch-json-state 'result))) +- ((result) +- ;; Parse a result +- (let ((result (notmuch-json-read notmuch-json-parser))) +- (case result +- ((retry) (setq done t)) +- ((end) (setq notmuch-json-state 'end)) +- (otherwise (with-current-buffer results-buf +- (funcall result-function result)))))) +- ((end) +- ;; Any trailing data is unexpected +- (notmuch-json-eof notmuch-json-parser) +- (setq done t))) +- (json-error +- ;; Do our best to resynchronize and ensure forward +- ;; progress +- (let ((bad (buffer-substring (line-beginning-position) +- (line-end-position)))) +- (forward-line) +- (with-current-buffer results-buf +- (funcall error-function "%s" bad)))))) +- ;; Clear out what we've parsed +- (delete-region (point-min) (point)))) +- + (defun notmuch-search-tag-all (&optional tag-changes) + "Add/remove tags from all messages in current search buffer. + +-- +1.7.9.1 + -- 2.26.2