tags:

views:

89

answers:

2

I have a pascal code file and need to parse it (using c#) and display all the public functions, my file looks something like that (not actual code):

public
  function Test(str: string):bool;
  function Test1(str: string):bool;
  function Test2(str,str1,str2,str3
                 str4: string):bool;
  function Test3(str: string):bool;
published

Notice Test2 there, and how it is in multiple lines. I can do some basic (very basic) regular expressions, but can't make anything that work with this. Basically what i need is: get all functions between the strings "public" and "published" and return it in one single line...

+5  A: 

This will be very hard to do, if not impossible. Just think about all the problems with strings and comments and escape sequences. If I were you I would use a Pascal parser instead.

Here's a page with lexx/yacc grammars for Pascal:

http://www.moorecad.com/standardpascal/yacclex.html

Here's some info on yacc and lexx:

http://dinosaur.compilertools.net/

With these tools premade for you, you can generate a parser in no time that will do a much better job than any regular expression.

DrJokepu
+1  A: 

I give you some regexs to get procedures and functions that have or don't have parameters

rproc1 = "(?<!\w)procedure\s+[\w\s.]+;"
rproc2 = "(?<!\w)procedure\s+[\w\s.]+\([\w\s,.=':;$/*()]*?\)\s*;"

rfunc1 = "(?<!\w)function\s+[\w\s.]+:\s*\w+\s*;"
rfunc2 = "(?<!\w)function\s+[\w\s.]+\([\w\s,.=':;$/*()]*?\)\s*:\s*\w+\s*;"

They are a sort of heuristics though. Don't expect to match every possible function declaration.

Nick D
Thanks Nick, combining rfunc1 and rfunc2 gave me the result that I needed :)
A parser would be a better solution, but anyway, I'm glad I could help.
Nick D