views:

133

answers:

3

I just started using LISP, coming from a background in C. So far its been fun, although with an incredible learning curve (I'm an emacs newbie too).

Anyway, I'm having a silly issue with the following code to parse include statements from c source - if anyone can comment on this and suggest a solution, it would help a lot.

(defun include-start ( line )
    (search "#include " line))

(defun get-include( line )
  (let ((s (include-start line)))
    (if (not (eq NIL s))
      (subseq line s (length line)))))

(get-include "#include <stdio.h>")

I expect the last line to return

"<stdio.h>"

However the actual result is

"#include <stdio.h>"

Any thoughts?

+1  A: 

I find replace-in-string much easier.

(replace-in-string "#include <stdio.h>" "#include +" "")
    => "<stdio.h>"

For your code, include-start returns the start of match, as the name suggests. You are looking for include-end which is probably (+ (include-start ....) (length ....))

Hemal Pandya
Where is 'replace-in-string' - is it a standard function? I'm using Closure Common Lisp on Darwin.
Justicle
Oh I thought you were using elisp.
Hemal Pandya
+4  A: 
(defun include-start (line)
  "returns the string position after the '#include ' directive or nil if none"
  (let ((search-string "#include "))
    (when (search search-string line)
      (length search-string))))

(defun get-include (line)
  (let ((s (include-start line)))
    (when s
      (subseq line s))))
Rainer Joswig
*slaps forehead* of course, my logic was just plain wrong - we'll see how day 2 goes :-)
Justicle
+1  A: 
(defun get-include (s)
   (subseq s (mismatch "#include " s)))
huaiyuan