[ragel-users] properties list

Torsten Curdt tcurdt at vafer.org
Tue Dec 1 01:46:29 UTC 2009


Hey folks,

I just stumbled across Ragel. In order to get a bit into it I though I
give it a try to parse some properties files looking like this:

--
  /* general comment */
  // another comment

  /* comment for the next property (key1) */
  "key1" = "value1"

  // comment for the next property (key2)
  "key2" = "value2"
--

I got something working but it's not even fully working to parse the
above. It would be great if you could point me into the right
direction. I will tomorrow continue to read up in the docs but here
are a couple of question that I did not immediately find.

1. Are actions the right way to access/extract the content of the
tokens? I would love to have the key/value pair available in the
machine definition of the assignment.
2. I've had a look at the C grammar but did not really understand how
the comment rules worked. I tried with that approach but I could not
capture and access the comment text.
3. Does whitespace has to be handled just as any other input? No
special treatment?
4. What about unicode support? I've read that UTF8 should be possible.
What about UTF16?

Sorry for the newbie questions. Here is my first try (of course
knowing that this still needs work)

--
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  %%{
  	machine strings;
    alphtype char;

  	newline = '\n' @{
  	  curline += 1;
  	};

    action key {
      printf("KEY: %c\n", fc);
    }

    action value {
      printf("VALUE: %c\n", fc);
    }

    action comment {
      printf("COMMENT: %c\n", fc);
    }

    # single line comment
    comment_line = '//' [^\n]* @comment '\n';

  	# quoted string
  	quoted_char = [^"\\] | newline;

    # assignment
    assignment = '"' quoted_char* @key '"' " "* "=" " "* '"'
quoted_char* @value '"';

  	main := comment_line? ( comment_line? assignment newline )*;

  }%%

  %% write data nofinal;

  void scanner()
  {
    char *s = "\"a\"=\"b\"\n \"c\" = \"d\" \n";

  	int act, curline = 1;
  	char *ts, *te = 0;

    int cs;
  	char *p = s;
  	char *pe = p + strlen(p) + 1;
  	char *eof = pe;

  	%% write init;
  	%% write exec;
  }

  int main()
  {
  	scanner();
  	return 0;
  }
--

Any comments appreciated.

cheers
--
Torsten




More information about the ragel-users mailing list