1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; .proto file parsing
18 (declaim (inline proto-whitespace-char-p))
19 (defun proto-whitespace-char-p (ch)
20 (declare #.$optimize-fast-unsafe)
21 (and ch (member ch '(#\space #\tab #\return #\newline))))
23 (declaim (inline proto-eol-char-p))
24 (defun proto-eol-char-p (ch)
25 (declare #.$optimize-fast-unsafe)
26 (and ch (member ch '(#\return #\newline))))
28 (declaim (inline proto-token-char-p))
29 (defun proto-token-char-p (ch)
30 (declare #.$optimize-fast-unsafe)
31 (and ch (or (alpha-char-p ch)
33 (member ch '(#\_ #\.)))))
36 (defun skip-whitespace (stream)
37 "Skip all the whitespace characters that are coming up in the stream."
38 (loop for ch = (peek-char nil stream nil)
39 until (or (null ch) (not (proto-whitespace-char-p ch)))
40 do (read-char stream nil)))
42 (defun expect-char (stream char &optional chars within)
43 "Expect to see 'char' as the next character in the stream; signal an error if it's not there.
44 Then skip all of the following whitespace.
45 The return value is the character that was eaten."
48 (member (peek-char nil stream nil) char)
49 (eql (peek-char nil stream nil) char))
50 (setq ch (read-char stream))
51 (error "No '~C' found~@[ within '~A'~] at position ~D"
52 char within (file-position stream)))
53 (maybe-skip-chars stream chars)
56 (defun maybe-skip-chars (stream chars)
57 "Skip some optional characters in the stream,
58 then skip all of the following whitespace."
59 (skip-whitespace stream)
62 (let ((ch (peek-char nil stream nil)))
63 (when (or (null ch) (not (member ch chars)))
64 (skip-whitespace stream)
65 (return-from maybe-skip-chars)))
69 ;;--- Collect the comment so we can attach it to its associated object
70 (defun maybe-skip-comments (stream)
71 "If what appears next in the stream is a comment, skip it and any following comments,
72 then skip any following whitespace."
74 (let ((ch (peek-char nil stream nil)))
75 (when (or (null ch) (not (eql ch #\/)))
76 (return-from maybe-skip-comments))
78 (case (peek-char nil stream nil)
80 (skip-line-comment stream))
82 (skip-block-comment stream))
84 (skip-whitespace stream)
85 (return-from maybe-skip-comments))
87 (error "Found a '~C' at position ~D to start a comment, but no following '~C' or '~C'"
88 #\/ (file-position stream) #\/ #\*))))))
90 (defun skip-line-comment (stream)
91 "Skip to the end of a line comment, that is, to the end of the line.
92 Then skip any following whitespace."
93 (loop for ch = (read-char stream nil)
94 until (or (null ch) (proto-eol-char-p ch)))
95 (skip-whitespace stream))
97 (defun skip-block-comment (stream)
98 "Skip to the end of a block comment, that is, until a '*/' is seen.
99 Then skip any following whitespace."
100 (loop for ch = (read-char stream nil)
102 (error "Premature end of file while skipping block comment"))
104 (eql (peek-char nil stream nil) #\/))
105 (read-char stream nil)
107 (skip-whitespace stream))
110 (defun parse-token (stream &optional additional-chars)
111 "Parse the next token in the stream, then skip the following whitespace.
112 The returned value is the token."
113 (when (let ((ch (peek-char nil stream nil)))
114 (or (proto-token-char-p ch) (member ch additional-chars)))
115 (loop for ch = (read-char stream nil)
116 for ch1 = (peek-char nil stream nil)
117 collect ch into token
119 (and (not (proto-token-char-p ch1))
120 (not (member ch1 additional-chars))))
122 (skip-whitespace stream)
123 (return (coerce token 'string))))))
125 (defun parse-parenthesized-token (stream)
126 "Parse the next token in the stream, then skip the following whitespace.
127 The token might be surrounded by parentheses.
128 The returned value is the token."
129 (let ((left (peek-char nil stream nil)))
132 (when (proto-token-char-p (peek-char nil stream nil))
133 (loop for ch = (read-char stream nil)
134 for ch1 = (peek-char nil stream nil)
135 collect ch into token
136 until (or (null ch1) (not (proto-token-char-p ch1)))
138 (skip-whitespace stream)
140 (expect-char stream #\)))
141 (return (coerce token 'string)))))))
143 (defun parse-token-or-string (stream)
144 (if (eql (peek-char nil stream nil) #\")
145 (values (parse-string stream) 'string)
146 (values (parse-token stream) 'symbol)))
148 (defun parse-string (stream)
149 "Parse the next quoted string in the stream, then skip the following whitespace.
150 The returned value is the string, without the quotation marks."
151 (loop with ch0 = (read-char stream nil)
152 for ch = (read-char stream nil)
153 until (or (null ch) (char= ch ch0))
155 do (setq ch (unescape-char stream))
156 collect ch into string
158 (skip-whitespace stream)
159 (if (eql (peek-char nil stream nil) ch0)
160 ;; If the next character is a quote character, that means
161 ;; we should go parse another string and concatenate it
162 (return (strcat (coerce string 'string) (parse-string stream)))
163 (return (coerce string 'string))))))
165 (defun unescape-char (stream)
166 "Parse the next \"escaped\" character from the stream."
167 (let ((ch (read-char stream nil)))
168 (assert (not (null ch)) ()
169 "End of stream reached while reading escaped character")
173 (let* ((d1 (digit-char-p (read-char stream) 16))
174 (d2 (digit-char-p (read-char stream) 16)))
175 (code-char (+ (* d1 16) d2))))
176 ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
177 (if (not (digit-char-p (peek-char nil stream nil)))
179 ;; Three octal digits
180 (let* ((d1 (digit-char-p ch 8))
181 (d2 (digit-char-p (read-char stream) 8))
182 (d3 (digit-char-p (read-char stream) 8)))
183 (code-char (+ (* d1 64) (* d2 8) d3)))))
193 (defun escape-char (ch)
194 "The inverse of 'unescape-char', for printing."
195 (if (and (standard-char-p ch) (graphic-char-p ch))
203 ((#\backspace) "\\b")
207 (format nil "\\x~2,'0X" (char-code ch))))))
209 (defun parse-signed-int (stream)
210 "Parse the next token in the stream as an integer, then skip the following whitespace.
211 The returned value is the integer."
212 (let* ((sign (if (eql (peek-char nil stream nil) #\-)
213 (progn (read-char stream) -1)
215 (int (parse-unsigned-int stream)))
218 (defun parse-unsigned-int (stream)
219 "Parse the next token in the stream as an integer, then skip the following whitespace.
220 The returned value is the integer."
221 (when (digit-char-p (peek-char nil stream nil))
222 (loop for ch = (read-char stream nil)
223 for ch1 = (peek-char nil stream nil)
224 collect ch into token
225 until (or (null ch1) (and (not (digit-char-p ch1)) (not (eql ch #\x))))
227 (skip-whitespace stream)
228 (let ((token (coerce token 'string)))
229 (if (starts-with token "0x")
230 (let ((*read-base* 16))
231 (return (parse-integer (subseq token 2))))
232 (return (parse-integer token))))))))
234 (defun parse-float (stream)
235 "Parse the next token in the stream as a float, then skip the following whitespace.
236 The returned value is the float."
237 (let ((number (parse-number stream)))
239 (coerce number 'float))))
241 (defun parse-number (stream)
242 (when (let ((ch (peek-char nil stream nil)))
243 (or (digit-char-p ch) (member ch '(#\- #\+ #\.))))
244 (let ((token (parse-token stream '(#\- #\+ #\.))))
246 (skip-whitespace stream)
247 (parse-numeric-string token)))))
249 (defun parse-numeric-string (string)
250 (cond ((starts-with string "0x")
251 (parse-integer (subseq string 2) :radix 16))
252 ((starts-with string "-0x")
253 (- (parse-integer (subseq string 3) :radix 16)))
255 (read-from-string string))))
258 ;;; The parser itself
260 (defun parse-schema-from-file (filename &key name class (conc-name ""))
261 "Parses the named file as a .proto file, and returns the Protobufs schema."
262 (with-open-file (stream filename
264 :external-format :utf-8
265 :element-type 'character)
266 (let ((*protobuf-pathname* (pathname stream))
267 (*compile-file-pathname* (pathname stream))
268 (*compile-file-truename* (truename stream)))
269 (parse-schema-from-stream stream
270 :name (or name (class-name->proto (pathname-name (pathname stream))))
271 :class (or class (kintern (pathname-name (pathname stream))))
272 :conc-name conc-name))))
274 ;; 'with-proto-source-location' counts on this being a 3-element list
275 ;; Yeah, it's a kludge, but we really don't need anything complicated for this
276 (defstruct (source-location (:type list) (:constructor %make-source-location))
281 (defun make-source-location (stream start end)
282 "Create a \"source locator\" for the stream at the current position.
283 With any luck, we can get meta-dot to pay attention to it."
284 (declare (ignore stream))
285 ;; Don't record source locations if we're not parsing from a file
286 (and *protobuf-pathname*
287 (%make-source-location :pathname *protobuf-pathname*
288 :start-pos start :end-pos end)))
290 ;; The syntax for Protocol Buffers is so simple that it doesn't seem worth
291 ;; writing a sophisticated parser
292 ;; Note that we don't put the result into *all-schemas*; that's done in 'define-schema'
293 (defun parse-schema-from-stream (stream &key name class (conc-name ""))
294 "Parses a top-level .proto file from the stream 'stream'.
295 Returns the protobuf schema that describes the .proto file."
296 (let* ((schema (make-instance 'protobuf-schema
300 (*protobuf-package* *package*)
301 (*protobuf-conc-name* conc-name))
303 (skip-whitespace stream)
304 (maybe-skip-comments stream)
305 (let ((char (peek-char nil stream nil)))
307 (remove-options schema "lisp_package")
308 (return-from parse-schema-from-stream schema))
309 ((proto-token-char-p char)
310 (let ((token (parse-token stream)))
311 (cond ((string= token "syntax")
312 (parse-proto-syntax stream schema))
313 ((string= token "package")
314 (parse-proto-package stream schema))
315 ((string= token "import")
316 (parse-proto-import stream schema))
317 ((string= token "option")
318 (let* ((option (parse-proto-option stream schema))
319 (name (and option (proto-name option)))
320 (value (and option (proto-value option))))
321 (when (and option (option-name= name "lisp_package"))
322 (let ((package (or (find-proto-package value)
323 ;; Try to put symbols into the right package
324 (make-package (string-upcase value) :use ())
325 *protobuf-package*)))
326 (setf (proto-lisp-package schema) value)
327 (setq *protobuf-package* package)))))
328 ((string= token "enum")
329 (parse-proto-enum stream schema))
330 ((string= token "extend")
331 (parse-proto-extend stream schema))
332 ((string= token "message")
333 (parse-proto-message stream schema))
334 ((string= token "service")
335 (parse-proto-service stream schema)))))
337 (error "Syntax error at position ~D" (file-position stream))))))))
339 (defun parse-proto-syntax (stream schema &optional (terminator #\;))
340 "Parse a Protobufs syntax line from 'stream'.
341 Updates the 'protobuf-schema' object to use the syntax."
342 (let ((syntax (prog2 (expect-char stream #\= () "syntax")
343 (parse-string stream)
344 (expect-char stream terminator () "syntax")
345 (maybe-skip-comments stream))))
346 (setf (proto-syntax schema) syntax)))
348 (defun parse-proto-package (stream schema &optional (terminator #\;))
349 "Parse a Protobufs package line from 'stream'.
350 Updates the 'protobuf-schema' object to use the package."
351 (check-type schema protobuf-schema)
352 (let* ((package (prog1 (parse-token stream)
353 (expect-char stream terminator () "package")
354 (maybe-skip-comments stream)))
355 (lisp-pkg (or (proto-lisp-package schema)
356 (substitute #\- #\_ package))))
357 (setf (proto-package schema) package)
358 (unless (proto-lisp-package schema)
359 (setf (proto-lisp-package schema) lisp-pkg))
360 (let ((package (or (find-proto-package lisp-pkg) *protobuf-package*)))
361 (setq *protobuf-package* package))))
363 (defun parse-proto-import (stream schema &optional (terminator #\;))
364 "Parse a Protobufs import line from 'stream'.
365 Updates the 'protobuf-schema' object to use the import."
366 (check-type schema protobuf-schema)
367 (let ((import (prog1 (parse-string stream)
368 (expect-char stream terminator () "import")
369 (maybe-skip-comments stream))))
370 (process-imports schema (list import))
371 (setf (proto-imports schema) (nconc (proto-imports schema) (list import)))))
373 (defun parse-proto-option (stream protobuf &optional (terminators '(#\;)))
374 "Parse a Protobufs option line from 'stream'.
375 Updates the 'protobuf-schema' (or message, service, method) to have the option."
376 (check-type protobuf (or null base-protobuf))
378 (key (prog1 (parse-parenthesized-token stream)
379 (expect-char stream #\= () "option")))
380 (val (prog1 (let ((ch (peek-char nil stream nil)))
382 (parse-string stream))
383 ((or (digit-char-p ch) (member ch '(#\- #\+ #\.)))
384 (parse-number stream))
386 (let ((message (find-message (or protobuf *protobuf*) key)))
388 ;; We've got a complex message as a value to an option
389 ;; This only shows up in custom optionss
390 (parse-text-format message :stream stream :parse-name nil)
391 ;; Who knows what to do? Skip the value
392 (skip-field stream))))
393 (t (kintern (parse-token stream)))))
394 (setq terminator (expect-char stream terminators () "option"))
395 (maybe-skip-comments stream)))
396 (option (make-instance 'protobuf-option
400 (setf (proto-options protobuf) (nconc (proto-options protobuf) (list option)))
401 (values option terminator))
403 ;; If nothing to graft the option into, just return it as the value
404 (values option terminator)))))
407 (defun parse-proto-enum (stream protobuf)
408 "Parse a Protobufs 'enum' from 'stream'.
409 Updates the 'protobuf-schema' or 'protobuf-message' object to have the enum."
410 (check-type protobuf (or protobuf-schema protobuf-message))
411 (let* ((loc (file-position stream))
412 (name (prog1 (parse-token stream)
413 (expect-char stream #\{ () "enum")
414 (maybe-skip-comments stream)))
415 (enum (make-instance 'protobuf-enum
416 :class (proto->class-name name *protobuf-package*)
418 :qualified-name (make-qualified-name protobuf name)
420 :source-location (make-source-location stream loc (i+ loc (length name))))))
422 (let ((name (parse-token stream)))
424 (expect-char stream #\} '(#\;) "enum")
425 (maybe-skip-comments stream)
426 (setf (proto-enums protobuf) (nconc (proto-enums protobuf) (list enum)))
427 (let ((type (find-option enum "lisp_name")))
429 (setf (proto-class enum) (make-lisp-symbol type))))
430 (let ((alias (find-option enum "lisp_alias")))
432 (setf (proto-alias-for enum) (make-lisp-symbol alias))))
433 (return-from parse-proto-enum enum))
434 (if (string= name "option")
435 (parse-proto-option stream enum)
436 (parse-proto-enum-value stream protobuf enum name))))))
438 (defun parse-proto-enum-value (stream protobuf enum name)
439 "Parse a Protobufs enum value from 'stream'.
440 Updates the 'protobuf-enum' object to have the enum value."
441 (declare (ignore protobuf))
442 (check-type enum protobuf-enum)
443 (expect-char stream #\= () "enum")
444 (let* ((idx (prog1 (parse-signed-int stream)
445 (expect-char stream #\; () "enum")
446 (maybe-skip-comments stream)))
447 (value (make-instance 'protobuf-enum-value
449 :qualified-name (make-qualified-name enum name)
451 :value (proto->enum-name name *protobuf-package*)
453 (setf (proto-values enum) (nconc (proto-values enum) (list value)))
457 (defun parse-proto-message (stream protobuf &optional name)
458 "Parse a Protobufs 'message' from 'stream'.
459 Updates the 'protobuf-schema' or 'protobuf-message' object to have the message."
460 (check-type protobuf (or protobuf-schema protobuf-message))
461 (let* ((loc (file-position stream))
462 (name (prog1 (or name (parse-token stream))
463 (expect-char stream #\{ () "message")
464 (maybe-skip-comments stream)))
465 (class (proto->class-name name *protobuf-package*))
466 (message (make-instance 'protobuf-message
469 :qualified-name (make-qualified-name protobuf name)
471 ;; Maybe force accessors for all slots
472 :conc-name (conc-name-for-type class *protobuf-conc-name*)
473 :source-location (make-source-location stream loc (i+ loc (length name)))))
474 (*protobuf* message))
476 (let ((token (parse-token stream)))
478 (expect-char stream #\} '(#\;) "message")
479 (maybe-skip-comments stream)
480 (setf (proto-messages protobuf) (nconc (proto-messages protobuf) (list message)))
481 (let ((type (find-option message "lisp_name")))
483 (setf (proto-class message) (make-lisp-symbol type))))
484 (let ((alias (find-option message "lisp_alias")))
486 (setf (proto-alias-for message) (make-lisp-symbol alias))))
487 (return-from parse-proto-message message))
488 (cond ((string= token "enum")
489 (parse-proto-enum stream message))
490 ((string= token "extend")
491 (parse-proto-extend stream message))
492 ((string= token "message")
493 (parse-proto-message stream message))
494 ((member token '("required" "optional" "repeated") :test #'string=)
495 (parse-proto-field stream message token))
496 ((string= token "option")
497 (parse-proto-option stream message))
498 ((string= token "extensions")
499 (parse-proto-extension stream message))
501 (error "Unrecognized token ~A at position ~D"
502 token (file-position stream))))))))
504 (defun parse-proto-extend (stream protobuf)
505 "Parse a Protobufs 'extend' from 'stream'.
506 Updates the 'protobuf-schema' or 'protobuf-message' object to have the message."
507 (check-type protobuf (or protobuf-schema protobuf-message))
508 (let* ((loc (file-position stream))
509 (name (prog1 (parse-token stream)
510 (expect-char stream #\{ () "extend")
511 (maybe-skip-comments stream)))
512 (message (find-message protobuf name))
513 (extends (and message
514 (make-instance 'protobuf-message
515 :class (proto-class message)
516 :name (proto-name message)
517 :qualified-name (proto-qualified-name message)
518 :parent (proto-parent message)
519 :alias-for (proto-alias-for message)
520 :conc-name (proto-conc-name message)
521 :enums (copy-list (proto-enums message))
522 :messages (copy-list (proto-messages message))
523 :fields (copy-list (proto-fields message))
524 :extensions (copy-list (proto-extensions message))
525 :message-type :extends ;this message is an extension
526 :source-location (make-source-location stream loc (i+ loc (length name))))))
527 (*protobuf* extends))
529 "There is no message named ~A to extend" name)
531 (let ((token (parse-token stream)))
533 (expect-char stream #\} '(#\;) "extend")
534 (maybe-skip-comments stream)
535 (setf (proto-messages protobuf) (nconc (proto-messages protobuf) (list extends)))
536 (setf (proto-extenders protobuf) (nconc (proto-extenders protobuf) (list extends)))
537 (let ((type (find-option extends "lisp_name")))
539 (setf (proto-class extends) (make-lisp-symbol type))))
540 (let ((alias (find-option extends "lisp_alias")))
542 (setf (proto-alias-for extends) (make-lisp-symbol alias))))
543 (return-from parse-proto-extend extends))
544 (cond ((member token '("required" "optional" "repeated") :test #'string=)
545 (let ((field (parse-proto-field stream extends token message)))
546 (setf (proto-extended-fields extends) (nconc (proto-extended-fields extends) (list field)))))
547 ((string= token "option")
548 (parse-proto-option stream extends))
550 (error "Unrecognized token ~A at position ~D"
551 token (file-position stream))))))))
553 (defun parse-proto-field (stream message required &optional extended-from)
554 "Parse a Protobufs field from 'stream'.
555 Updates the 'protobuf-message' object to have the field."
556 (check-type message protobuf-message)
557 (let ((type (parse-token stream)))
558 (if (string= type "group")
559 (parse-proto-group stream message required extended-from)
560 (let* ((name (prog1 (parse-token stream)
561 (expect-char stream #\= () "message")))
562 (idx (parse-unsigned-int stream))
563 (opts (prog1 (parse-proto-field-options stream)
564 (expect-char stream #\; () "message")
565 (maybe-skip-comments stream)))
566 (packed (find-option opts "packed"))
567 (ptype (if (member type '("int32" "int64" "uint32" "uint64" "sint32" "sint64"
568 "fixed32" "fixed64" "sfixed32" "sfixed64"
569 "string" "bytes" "bool" "float" "double") :test #'string=)
572 (class (if (keywordp ptype) ptype (proto->class-name type *protobuf-package*)))
573 (slot (proto->slot-name name *protobuf-package*))
574 (reqd (kintern required))
575 (field (make-instance 'protobuf-field
579 :qualified-name (make-qualified-name message name)
581 ;; One of :required, :optional or :repeated
585 ;; Fields parsed from .proto files usually get an accessor
586 :reader (let ((conc-name (proto-conc-name message)))
588 (intern (format nil "~A~A" conc-name slot) *protobuf-package*)))
589 :default (multiple-value-bind (default type default-p)
590 (find-option opts "default")
591 (declare (ignore type))
594 (if (eq reqd :repeated) $empty-list $empty-default)))
595 :packed (and packed (boolean-true-p packed))
596 :message-type (proto-message-type message)
597 :options (remove-options opts "default" "packed"))))
599 (assert (index-within-extensions-p idx extended-from) ()
600 "The index ~D is not in range for extending ~S"
601 idx (proto-class extended-from)))
602 (let ((slot (find-option opts "lisp_name")))
604 (setf (proto-value field) (make-lisp-symbol type))))
605 (setf (proto-fields message) (nconc (proto-fields message) (list field)))
608 (defun parse-proto-group (stream message required &optional extended-from)
609 "Parse a (deprecated) Protobufs group from 'stream'.
610 Updates the 'protobuf-message' object to have the group type and field."
611 (check-type message protobuf-message)
612 (let* ((type (prog1 (parse-token stream)
613 (expect-char stream #\= () "message")))
614 (name (slot-name->proto (proto->slot-name type)))
615 (idx (parse-unsigned-int stream))
616 (msg (parse-proto-message stream message type))
617 (class (proto->class-name type *protobuf-package*))
618 (slot (proto->slot-name name *protobuf-package*))
619 (field (make-instance 'protobuf-field
623 :qualified-name (make-qualified-name message name)
625 :required (kintern required)
628 ;; Groups parsed from .proto files usually get an accessor
629 :reader (let ((conc-name (proto-conc-name message)))
631 (intern (format nil "~A~A" conc-name slot) *protobuf-package*)))
632 :message-type :group)))
633 (setf (proto-message-type msg) :group)
635 (assert (index-within-extensions-p idx extended-from) ()
636 "The index ~D is not in range for extending ~S"
637 idx (proto-class extended-from)))
638 (setf (proto-fields message) (nconc (proto-fields message) (list field)))
641 (defun parse-proto-field-options (stream)
642 "Parse any options in a Protobufs field from 'stream'.
643 Returns a list of 'protobuf-option' objects."
644 (with-collectors ((options collect-option))
645 (let ((terminator nil))
647 (cond ((eql (peek-char nil stream nil) #\[)
648 (expect-char stream #\[ () "message"))
649 ((eql terminator #\,))
651 (return-from parse-proto-field-options options)))
652 (multiple-value-bind (option term)
653 (parse-proto-option stream nil '(#\] #\,))
654 (setq terminator term)
655 (collect-option option))))))
657 (defun parse-proto-extension (stream message)
658 (check-type message protobuf-message)
659 (let* ((from (parse-unsigned-int stream))
660 (token (parse-token stream))
661 (to (let ((ch (peek-char nil stream nil)))
662 (cond ((digit-char-p (peek-char nil stream nil))
663 (parse-unsigned-int stream))
665 (t (parse-token stream))))))
666 (expect-char stream #\; () "message")
667 (assert (or (null token) (string= token "to")) ()
668 "Expected 'to' in 'extensions' at position ~D" (file-position stream))
669 (assert (or (integerp to) (string= to "max")) ()
670 "Extension value is not an integer or 'max' as position ~D" (file-position stream))
671 (let ((extension (make-instance 'protobuf-extension
673 :to (if (integerp to) to #.(1- (ash 1 29))))))
674 (setf (proto-extensions message)
675 (nconc (proto-extensions message)
680 (defun parse-proto-service (stream schema)
681 "Parse a Protobufs 'service' from 'stream'.
682 Updates the 'protobuf-schema' object to have the service."
683 (check-type schema protobuf-schema)
684 (let* ((loc (file-position stream))
685 (name (prog1 (parse-token stream)
686 (expect-char stream #\{ () "service")
687 (maybe-skip-comments stream)))
688 (service (make-instance 'protobuf-service
689 :class (proto->class-name name *protobuf-package*)
691 :qualified-name (make-qualified-name *protobuf* name)
693 :source-location (make-source-location stream loc (i+ loc (length name)))))
696 (let ((token (parse-token stream)))
698 (expect-char stream #\} '(#\;) "service")
699 (maybe-skip-comments stream)
700 (setf (proto-services schema) (nconc (proto-services schema) (list service)))
701 (return-from parse-proto-service service))
702 (cond ((string= token "option")
703 (parse-proto-option stream service))
704 ((string= token "rpc")
705 (parse-proto-method stream service (iincf index)))
707 (error "Unrecognized token ~A at position ~D"
708 token (file-position stream))))))))
710 (defun parse-proto-method (stream service index)
711 "Parse a Protobufs method from 'stream'.
712 Updates the 'protobuf-service' object to have the method."
713 (check-type service protobuf-service)
714 (let* ((loc (file-position stream))
715 (name (parse-token stream))
716 (in (prog2 (expect-char stream #\( () "service")
718 (expect-char stream #\) () "service")))
719 (ret (parse-token stream)) ;should be "=>"
720 (out (prog2 (expect-char stream #\( () "service")
722 (expect-char stream #\) () "service")))
723 (strm (parse-token stream)) ;might be "streams"
724 (strm (and strm (string= strm "streams")
725 (prog2 (expect-char stream #\( () "service")
727 (expect-char stream #\) () "service"))))
728 (opts (let ((opts (parse-proto-method-options stream)))
729 (when (or (null opts) (eql (peek-char nil stream nil) #\;))
730 (expect-char stream #\; () "service"))
731 (maybe-skip-comments stream)
733 (stub (proto->class-name name *protobuf-package*))
734 (method (make-instance 'protobuf-method
737 :qualified-name (make-qualified-name *protobuf* name)
739 :input-type (proto->class-name in *protobuf-package*)
741 :output-type (proto->class-name out *protobuf-package*)
743 :streams-type (and strm (proto->class-name strm *protobuf-package*))
747 :source-location (make-source-location stream loc (i+ loc (length name))))))
748 (assert (string= ret "returns") ()
749 "Syntax error in 'message' at position ~D" (file-position stream))
750 (let* ((name (find-option method "lisp_name"))
751 (stub (or (and name (make-lisp-symbol name))
753 (setf (proto-class method) stub
754 (proto-client-stub method) stub
755 (proto-server-stub method) (intern (format nil "~A-~A" 'do stub) *protobuf-package*)))
756 (setf (proto-methods service) (nconc (proto-methods service) (list method)))
759 (defun parse-proto-method-options (stream)
760 "Parse any options in a Protobufs method from 'stream'.
761 Returns a list of 'protobuf-option' objects."
762 (when (eql (peek-char nil stream nil) #\{)
763 (expect-char stream #\{ () "service")
764 (maybe-skip-comments stream)
765 (with-collectors ((options collect-option))
767 (when (eql (peek-char nil stream nil) #\})
769 (assert (string= (parse-token stream) "option") ()
770 "Syntax error in 'message' at position ~D" (file-position stream))
771 (collect-option (parse-proto-option stream nil)))
772 (expect-char stream #\} '(#\;) "service")
773 (maybe-skip-comments stream)