ebnf-bnf.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. ;;; ebnf-bnf.el --- parser for EBNF
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  4. ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  5. ;; Keywords: wp, ebnf, PostScript
  6. ;; Version: 1.10
  7. ;; Package: ebnf2ps
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;
  23. ;; This is part of ebnf2ps package.
  24. ;;
  25. ;; This package defines a parser for EBNF.
  26. ;;
  27. ;; See ebnf2ps.el for documentation.
  28. ;;
  29. ;;
  30. ;; EBNF Syntax
  31. ;; -----------
  32. ;;
  33. ;; The current EBNF that ebnf2ps accepts has the following constructions:
  34. ;;
  35. ;; ; comment (until end of line)
  36. ;; A non-terminal
  37. ;; "C" terminal
  38. ;; ?C? special
  39. ;; $A default non-terminal
  40. ;; $"C" default terminal
  41. ;; $?C? default special
  42. ;; A = B. production (A is the header and B the body)
  43. ;; C D sequence (C occurs before D)
  44. ;; C | D alternative (C or D occurs)
  45. ;; A - B exception (A excluding B, B without any non-terminal)
  46. ;; n * A repetition (A repeats at least n (integer) times)
  47. ;; n * n A repetition (A repeats exactly n (integer) times)
  48. ;; n * m A repetition (A repeats at least n (integer) and at most
  49. ;; m (integer) times)
  50. ;; (C) group (expression C is grouped together)
  51. ;; [C] optional (C may or not occurs)
  52. ;; C+ one or more occurrences of C
  53. ;; {C}+ one or more occurrences of C
  54. ;; {C}* zero or more occurrences of C
  55. ;; {C} zero or more occurrences of C
  56. ;; C / D equivalent to: C {D C}*
  57. ;; {C || D}+ equivalent to: C {D C}*
  58. ;; {C || D}* equivalent to: [C {D C}*]
  59. ;; {C || D} equivalent to: [C {D C}*]
  60. ;;
  61. ;; The EBNF syntax written using the notation above is:
  62. ;;
  63. ;; EBNF = {production}+.
  64. ;;
  65. ;; production = non_terminal "=" body ".". ;; production
  66. ;;
  67. ;; body = {sequence || "|"}*. ;; alternative
  68. ;;
  69. ;; sequence = {exception}*. ;; sequence
  70. ;;
  71. ;; exception = repeat [ "-" repeat]. ;; exception
  72. ;;
  73. ;; repeat = [ integer "*" [ integer ]] term. ;; repetition
  74. ;;
  75. ;; term = factor
  76. ;; | [factor] "+" ;; one-or-more
  77. ;; | [factor] "/" [factor] ;; one-or-more
  78. ;; .
  79. ;;
  80. ;; factor = [ "$" ] "\"" terminal "\"" ;; terminal
  81. ;; | [ "$" ] non_terminal ;; non-terminal
  82. ;; | [ "$" ] "?" special "?" ;; special
  83. ;; | "(" body ")" ;; group
  84. ;; | "[" body "]" ;; zero-or-one
  85. ;; | "{" body [ "||" body ] "}+" ;; one-or-more
  86. ;; | "{" body [ "||" body ] "}*" ;; zero-or-more
  87. ;; | "{" body [ "||" body ] "}" ;; zero-or-more
  88. ;; .
  89. ;;
  90. ;; non_terminal = "[!#%&'*-,0-:<>@-Z\\\\^-z~\\240-\\377]+".
  91. ;; ;; that is, a valid non_terminal accepts decimal digits, letters (upper
  92. ;; ;; and lower), 8-bit accentuated characters,
  93. ;; ;; "!", "#", "%", "&", "'", "*", "+", ",", ":",
  94. ;; ;; "<", ">", "@", "\", "^", "_", "`" and "~".
  95. ;;
  96. ;; terminal = "\\([^\"\\]\\|\\\\[ -~\\240-\\377]\\)+".
  97. ;; ;; that is, a valid terminal accepts any printable character (including
  98. ;; ;; 8-bit accentuated characters) except `"', as `"' is used to delimit a
  99. ;; ;; terminal. Also, accepts escaped characters, that is, a character
  100. ;; ;; pair starting with `\' followed by a printable character, for
  101. ;; ;; example: \", \\.
  102. ;;
  103. ;; special = "[^?\\000-\\010\\012-\\037\\177-\\237]*".
  104. ;; ;; that is, a valid special accepts any printable character (including
  105. ;; ;; 8-bit accentuated characters) and tabs except `?', as `?' is used to
  106. ;; ;; delimit a special.
  107. ;;
  108. ;; integer = "[0-9]+".
  109. ;; ;; that is, an integer is a sequence of one or more decimal digits.
  110. ;;
  111. ;; comment = ";" "[^\\n\\000-\\010\\016-\\037\\177-\\237]*" "\\n".
  112. ;; ;; that is, a comment starts with the character `;' and terminates at end
  113. ;; ;; of line. Also, it only accepts printable characters (including 8-bit
  114. ;; ;; accentuated characters) and tabs.
  115. ;;
  116. ;;
  117. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  118. ;;; Code:
  119. (require 'ebnf-otz)
  120. (defvar ebnf-bnf-lex nil
  121. "Value returned by `ebnf-bnf-lex' function.")
  122. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  123. ;; Syntactic analyzer
  124. ;;; EBNF = {production}+.
  125. (defun ebnf-bnf-parser (start)
  126. "EBNF parser."
  127. (let ((total (+ (- ebnf-limit start) 1))
  128. (bias (1- start))
  129. (origin (point))
  130. prod-list token rule)
  131. (goto-char start)
  132. (setq token (ebnf-bnf-lex))
  133. (and (eq token 'end-of-input)
  134. (error "Invalid EBNF file format"))
  135. (while (not (eq token 'end-of-input))
  136. (ebnf-message-float
  137. "Parsing...%s%%"
  138. (/ (* (- (point) bias) 100.0) total))
  139. (setq token (ebnf-production token)
  140. rule (cdr token)
  141. token (car token))
  142. (or (ebnf-add-empty-rule-list rule)
  143. (setq prod-list (cons rule prod-list))))
  144. (goto-char origin)
  145. prod-list))
  146. ;;; production = non-terminal "=" body ".".
  147. (defun ebnf-production (token)
  148. (let ((header ebnf-bnf-lex)
  149. (action ebnf-action)
  150. body)
  151. (setq ebnf-action nil)
  152. (or (eq token 'non-terminal)
  153. (error "Invalid header production"))
  154. (or (eq (ebnf-bnf-lex) 'equal)
  155. (error "Invalid production: missing `='"))
  156. (setq body (ebnf-body))
  157. (or (eq (car body) 'period)
  158. (error "Invalid production: missing `.'"))
  159. (setq body (cdr body))
  160. (ebnf-eps-add-production header)
  161. (cons (ebnf-bnf-lex)
  162. (ebnf-make-production header body action))))
  163. ;;; body = {sequence || "|"}*.
  164. (defun ebnf-body ()
  165. (let (body sequence)
  166. (while (eq (car (setq sequence (ebnf-sequence))) 'alternative)
  167. (setq sequence (cdr sequence)
  168. body (cons sequence body)))
  169. (ebnf-token-alternative body sequence)))
  170. ;;; sequence = {exception}*.
  171. (defun ebnf-sequence ()
  172. (let ((token (ebnf-bnf-lex))
  173. seq term)
  174. (while (setq term (ebnf-exception token)
  175. token (car term)
  176. term (cdr term))
  177. (setq seq (cons term seq)))
  178. (cons token
  179. (ebnf-token-sequence seq))))
  180. ;;; exception = repeat [ "-" repeat].
  181. (defun ebnf-exception (token)
  182. (let ((term (ebnf-repeat token)))
  183. (if (not (eq (car term) 'except))
  184. ;; repeat
  185. term
  186. ;; repeat - repeat
  187. (let ((exception (ebnf-repeat (ebnf-bnf-lex))))
  188. (ebnf-no-non-terminal (cdr exception))
  189. (ebnf-token-except (cdr term) exception)))))
  190. (defun ebnf-no-non-terminal (node)
  191. (and (vectorp node)
  192. (let ((kind (ebnf-node-kind node)))
  193. (cond
  194. ((eq kind 'ebnf-generate-non-terminal)
  195. (error "Exception sequence should not contain a non-terminal"))
  196. ((eq kind 'ebnf-generate-repeat)
  197. (ebnf-no-non-terminal (ebnf-node-separator node)))
  198. ((memq kind '(ebnf-generate-optional ebnf-generate-except))
  199. (ebnf-no-non-terminal (ebnf-node-list node)))
  200. ((memq kind '(ebnf-generate-one-or-more ebnf-generate-zero-or-more))
  201. (ebnf-no-non-terminal (ebnf-node-list node))
  202. (ebnf-no-non-terminal (ebnf-node-separator node)))
  203. ((memq kind '(ebnf-generate-alternative ebnf-generate-sequence))
  204. (let ((seq (ebnf-node-list node)))
  205. (while seq
  206. (ebnf-no-non-terminal (car seq))
  207. (setq seq (cdr seq)))))
  208. ))))
  209. ;;; repeat = [ integer "*" [ integer ]] term.
  210. (defun ebnf-repeat (token)
  211. (if (not (eq token 'integer))
  212. (ebnf-term token)
  213. (let ((times ebnf-bnf-lex)
  214. upper)
  215. (or (eq (ebnf-bnf-lex) 'repeat)
  216. (error "Missing `*'"))
  217. (setq token (ebnf-bnf-lex))
  218. (when (eq token 'integer)
  219. (setq upper ebnf-bnf-lex
  220. token (ebnf-bnf-lex)))
  221. (ebnf-token-repeat times (ebnf-term token) upper))))
  222. ;;; term = factor
  223. ;;; | [factor] "+" ;; one-or-more
  224. ;;; | [factor] "/" [factor] ;; one-or-more
  225. ;;; .
  226. (defun ebnf-term (token)
  227. (let ((factor (ebnf-factor token)))
  228. (and factor
  229. (setq token (ebnf-bnf-lex)))
  230. (cond
  231. ;; [factor] +
  232. ((eq token 'one-or-more)
  233. (cons (ebnf-bnf-lex)
  234. (and factor
  235. (let ((kind (ebnf-node-kind factor)))
  236. (cond
  237. ;; { A }+ + ==> { A }+
  238. ;; { A }* + ==> { A }*
  239. ((memq kind '(ebnf-generate-zero-or-more
  240. ebnf-generate-one-or-more))
  241. factor)
  242. ;; [ A ] + ==> { A }*
  243. ((eq kind 'ebnf-generate-optional)
  244. (ebnf-make-zero-or-more (list factor)))
  245. ;; A +
  246. (t
  247. (ebnf-make-one-or-more (list factor)))
  248. )))))
  249. ;; [factor] / [factor]
  250. ((eq token 'list)
  251. (setq token (ebnf-bnf-lex))
  252. (let ((sep (ebnf-factor token)))
  253. (and sep
  254. (setq factor (or factor (ebnf-make-empty))))
  255. (cons (if sep
  256. (ebnf-bnf-lex)
  257. token)
  258. (and factor
  259. (ebnf-make-one-or-more factor sep)))))
  260. ;; factor
  261. (t
  262. (cons token factor))
  263. )))
  264. ;;; factor = [ "$" ] "\"" terminal "\"" ;; terminal
  265. ;;; | [ "$" ] non_terminal ;; non-terminal
  266. ;;; | [ "$" ] "?" special "?" ;; special
  267. ;;; | "(" body ")" ;; group
  268. ;;; | "[" body "]" ;; zero-or-one
  269. ;;; | "{" body [ "||" body ] "}+" ;; one-or-more
  270. ;;; | "{" body [ "||" body ] "}*" ;; zero-or-more
  271. ;;; | "{" body [ "||" body ] "}" ;; zero-or-more
  272. ;;; .
  273. (defun ebnf-factor (token)
  274. (cond
  275. ;; terminal
  276. ((eq token 'terminal)
  277. (ebnf-make-terminal ebnf-bnf-lex))
  278. ;; non-terminal
  279. ((eq token 'non-terminal)
  280. (ebnf-make-non-terminal ebnf-bnf-lex))
  281. ;; special
  282. ((eq token 'special)
  283. (ebnf-make-special ebnf-bnf-lex))
  284. ;; group
  285. ((eq token 'begin-group)
  286. (let ((body (ebnf-body)))
  287. (or (eq (car body) 'end-group)
  288. (error "Missing `)'"))
  289. (cdr body)))
  290. ;; optional
  291. ((eq token 'begin-optional)
  292. (let ((body (ebnf-body)))
  293. (or (eq (car body) 'end-optional)
  294. (error "Missing `]'"))
  295. (ebnf-token-optional (cdr body))))
  296. ;; list
  297. ((eq token 'begin-list)
  298. (let* ((body (ebnf-body))
  299. (token (car body))
  300. (list-part (cdr body))
  301. sep-part)
  302. (and (eq token 'list-separator)
  303. ;; { A || B }
  304. (setq body (ebnf-body) ; get separator
  305. token (car body)
  306. sep-part (cdr body)))
  307. (cond
  308. ;; { A }+
  309. ((eq token 'end-one-or-more)
  310. (ebnf-make-one-or-more list-part sep-part))
  311. ;; { A }*
  312. ((eq token 'end-zero-or-more)
  313. (ebnf-make-zero-or-more list-part sep-part))
  314. (t
  315. (error "Missing `}+', `}*' or `}'"))
  316. )))
  317. ;; no term
  318. (t
  319. nil)
  320. ))
  321. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  322. ;; Lexical analyzer
  323. (defconst ebnf-bnf-token-table (make-vector 256 'error)
  324. "Vector used to map characters to a lexical token.")
  325. (defun ebnf-bnf-initialize ()
  326. "Initialize EBNF token table."
  327. ;; control character & control 8-bit character are set to `error'
  328. (let ((char ?\040))
  329. ;; printable character:
  330. (while (< char ?\060)
  331. (aset ebnf-bnf-token-table char 'non-terminal)
  332. (setq char (1+ char)))
  333. ;; digits:
  334. (while (< char ?\072)
  335. (aset ebnf-bnf-token-table char 'integer)
  336. (setq char (1+ char)))
  337. ;; printable character:
  338. (while (< char ?\177)
  339. (aset ebnf-bnf-token-table char 'non-terminal)
  340. (setq char (1+ char)))
  341. ;; European 8-bit accentuated characters:
  342. (setq char ?\240)
  343. (while (< char ?\400)
  344. (aset ebnf-bnf-token-table char 'non-terminal)
  345. (setq char (1+ char)))
  346. ;; Override space characters:
  347. (aset ebnf-bnf-token-table ?\013 'space) ; [VT] vertical tab
  348. (aset ebnf-bnf-token-table ?\n 'space) ; [NL] linefeed
  349. (aset ebnf-bnf-token-table ?\r 'space) ; [CR] carriage return
  350. (aset ebnf-bnf-token-table ?\t 'space) ; [HT] horizontal tab
  351. (aset ebnf-bnf-token-table ?\ 'space) ; [SP] space
  352. ;; Override form feed character:
  353. (aset ebnf-bnf-token-table ?\f 'form-feed) ; [FF] form feed
  354. ;; Override other lexical characters:
  355. (aset ebnf-bnf-token-table ?\" 'terminal)
  356. (aset ebnf-bnf-token-table ?\? 'special)
  357. (aset ebnf-bnf-token-table ?\( 'begin-group)
  358. (aset ebnf-bnf-token-table ?\) 'end-group)
  359. (aset ebnf-bnf-token-table ?* 'repeat)
  360. (aset ebnf-bnf-token-table ?- 'except)
  361. (aset ebnf-bnf-token-table ?= 'equal)
  362. (aset ebnf-bnf-token-table ?\[ 'begin-optional)
  363. (aset ebnf-bnf-token-table ?\] 'end-optional)
  364. (aset ebnf-bnf-token-table ?\{ 'begin-list)
  365. (aset ebnf-bnf-token-table ?| 'alternative)
  366. (aset ebnf-bnf-token-table ?\} 'end-list)
  367. (aset ebnf-bnf-token-table ?/ 'list)
  368. (aset ebnf-bnf-token-table ?+ 'one-or-more)
  369. (aset ebnf-bnf-token-table ?$ 'default)
  370. ;; Override comment character:
  371. (aset ebnf-bnf-token-table ebnf-lex-comment-char 'comment)
  372. ;; Override end of production character:
  373. (aset ebnf-bnf-token-table ebnf-lex-eop-char 'period)))
  374. ;; replace the range "\240-\377" (see `ebnf-range-regexp').
  375. (defconst ebnf-bnf-non-terminal-chars
  376. (ebnf-range-regexp "!#%&'*-,0-:<>@-Z\\\\^-z~" ?\240 ?\377))
  377. (defun ebnf-bnf-lex ()
  378. "Lexical analyzer for EBNF.
  379. Return a lexical token.
  380. See documentation for variable `ebnf-bnf-lex'."
  381. (if (>= (point) ebnf-limit)
  382. 'end-of-input
  383. (let (token)
  384. ;; skip spaces and comments
  385. (while (if (> (following-char) 255)
  386. (progn
  387. (setq token 'error)
  388. nil)
  389. (setq token (aref ebnf-bnf-token-table (following-char)))
  390. (cond
  391. ((eq token 'space)
  392. (skip-chars-forward " \013\n\r\t" ebnf-limit)
  393. (< (point) ebnf-limit))
  394. ((eq token 'comment)
  395. (ebnf-bnf-skip-comment))
  396. ((eq token 'form-feed)
  397. (forward-char)
  398. (setq ebnf-action 'form-feed))
  399. (t nil)
  400. )))
  401. (setq ebnf-default-p nil)
  402. (cond
  403. ;; end of input
  404. ((>= (point) ebnf-limit)
  405. 'end-of-input)
  406. ;; error
  407. ((eq token 'error)
  408. (error "Invalid character"))
  409. ;; default
  410. ((eq token 'default)
  411. (forward-char)
  412. (if (memq (aref ebnf-bnf-token-table (following-char))
  413. '(terminal non-terminal special))
  414. (prog1
  415. (ebnf-bnf-lex)
  416. (setq ebnf-default-p t))
  417. (error "Invalid `default' element")))
  418. ;; integer
  419. ((eq token 'integer)
  420. (setq ebnf-bnf-lex (ebnf-buffer-substring "0-9"))
  421. 'integer)
  422. ;; special: ?special?
  423. ((eq token 'special)
  424. (setq ebnf-bnf-lex (concat (and ebnf-special-show-delimiter "?")
  425. (ebnf-string " ->@-~" ?\? "special")
  426. (and ebnf-special-show-delimiter "?")))
  427. 'special)
  428. ;; terminal: "string"
  429. ((eq token 'terminal)
  430. (setq ebnf-bnf-lex (ebnf-unescape-string (ebnf-get-string)))
  431. 'terminal)
  432. ;; non-terminal or terminal
  433. ((eq token 'non-terminal)
  434. (setq ebnf-bnf-lex (ebnf-buffer-substring ebnf-bnf-non-terminal-chars))
  435. (let ((case-fold-search ebnf-case-fold-search)
  436. match)
  437. (if (and ebnf-terminal-regexp
  438. (setq match (string-match ebnf-terminal-regexp
  439. ebnf-bnf-lex))
  440. (zerop match)
  441. (= (match-end 0) (length ebnf-bnf-lex)))
  442. 'terminal
  443. 'non-terminal)))
  444. ;; end of list: }+, }*, }
  445. ((eq token 'end-list)
  446. (forward-char)
  447. (cond
  448. ((= (following-char) ?+)
  449. (forward-char)
  450. 'end-one-or-more)
  451. ((= (following-char) ?*)
  452. (forward-char)
  453. 'end-zero-or-more)
  454. (t
  455. 'end-zero-or-more)
  456. ))
  457. ;; alternative: |, ||
  458. ((eq token 'alternative)
  459. (forward-char)
  460. (if (/= (following-char) ?|)
  461. 'alternative
  462. (forward-char)
  463. 'list-separator))
  464. ;; miscellaneous: {, (, ), [, ], ., =, /, +, -, *
  465. (t
  466. (forward-char)
  467. token)
  468. ))))
  469. ;; replace the range "\177-\237" (see `ebnf-range-regexp').
  470. (defconst ebnf-bnf-comment-chars
  471. (ebnf-range-regexp "^\n\000-\010\016-\037" ?\177 ?\237))
  472. (defun ebnf-bnf-skip-comment ()
  473. (forward-char)
  474. (cond
  475. ;; open EPS file
  476. ((and ebnf-eps-executing (= (following-char) ?\[))
  477. (ebnf-eps-add-context (ebnf-bnf-eps-filename)))
  478. ;; close EPS file
  479. ((and ebnf-eps-executing (= (following-char) ?\]))
  480. (ebnf-eps-remove-context (ebnf-bnf-eps-filename)))
  481. ;; EPS header
  482. ((and ebnf-eps-executing (= (following-char) ?H))
  483. (ebnf-eps-header-comment (ebnf-bnf-eps-filename)))
  484. ;; EPS footer
  485. ((and ebnf-eps-executing (= (following-char) ?F))
  486. (ebnf-eps-footer-comment (ebnf-bnf-eps-filename)))
  487. ;; any other action in comment
  488. (t
  489. (setq ebnf-action (aref ebnf-comment-table (following-char)))
  490. (skip-chars-forward ebnf-bnf-comment-chars ebnf-limit))
  491. )
  492. ;; check for a valid end of comment
  493. (cond ((>= (point) ebnf-limit)
  494. nil)
  495. ((= (following-char) ?\n)
  496. (forward-char)
  497. t)
  498. (t
  499. (error "Invalid character"))
  500. ))
  501. (defun ebnf-bnf-eps-filename ()
  502. (forward-char)
  503. (ebnf-buffer-substring ebnf-bnf-comment-chars))
  504. (defun ebnf-unescape-string (str)
  505. (let* ((len (length str))
  506. (size (1- len))
  507. (istr 0)
  508. (n-esc 0))
  509. ;; count number of escapes
  510. (while (< istr size)
  511. (setq istr (+ istr
  512. (if (= (aref str istr) ?\\)
  513. (progn
  514. (setq n-esc (1+ n-esc))
  515. 2)
  516. 1))))
  517. (if (zerop n-esc)
  518. ;; no escapes
  519. str
  520. ;; at least one escape
  521. (let ((new (make-string (- len n-esc) ?\ ))
  522. (inew 0))
  523. ;; eliminate all escapes
  524. (setq istr 0)
  525. (while (> n-esc 0)
  526. (and (= (aref str istr) ?\\)
  527. (setq istr (1+ istr)
  528. n-esc (1- n-esc)))
  529. (aset new inew (aref str istr))
  530. (setq inew (1+ inew)
  531. istr (1+ istr)))
  532. ;; remaining string has no escape
  533. (while (< istr len)
  534. (aset new inew (aref str istr))
  535. (setq inew (1+ inew)
  536. istr (1+ istr)))
  537. new))))
  538. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  539. (provide 'ebnf-bnf)
  540. ;;; ebnf-bnf.el ends here