[antlr-interest] iniFile grammer
Bart Kiers
bkiers at gmail.com
Wed Aug 17 11:10:02 PDT 2011
Hi Романов,
How about something like this:
grammar INIFile;
parse
: Comment* section+ EOF
;
section
: SectionStart (property | Comment)*
;
property
: NameOrValue (Assign valueList?)? eol
;
valueList
: (Separator | NameOrValue)+
;
eol
: LineBreak
| Comment
| EOF
;
Comment
: '#' ~('\r' | '\n')* LineBreak
;
SectionStart
: '[' ~(']')+ ']' Spaces? LineBreak
;
Separator
: ';'
;
Assign
: '='
;
NameOrValue
: ~(' ' | '\t' | '=' | '\r' | '\n' | '#' | ';') ~('=' | '\r' | '\n' | '#'
| ';')*
;
LineBreak
: '\r'? '\n'
| '\r'
;
Spaces
: (' ' | '\t')+ {$channel=HIDDEN;}
;
Regards,
Bart.
On Wed, Aug 17, 2011 at 2:02 PM, Романов Артем <arteminus at yandex.ru> wrote:
> I try define iniFile grammer(keys contains few subvalues).
> I defined it from C# Regex:
> Regex commentLine = new Regex(@"^\s*#(?<comment>.*)",
> RegexOptions.Compiled);
> Regex sectionLine = new Regex(@"^\s*\[(?<section>.*)\].*",
> RegexOptions.Compiled);
> //Regex recordLine = new
> Regex(@"^\s*(?<key>[^#[=\s]+)\s*=?\s*(?<values>[^#]*)(#(?<comment>.*))?",
> RegexOptions.Compiled);
> Regex recordLine2 = new
> Regex(@"^\s*(?<key>[^#[=\s]+)\s*=?\s*((?<value>[^;\#]*);)*(?<endValue>[^;\#]*[^;\s\#]+)?\s*(#(?<comment>.*))?",
> RegexOptions.Compiled);
> foreach(var c in recordLine2.Math(string).Groups["value"].Captures)
> //access to each value of key
> Sample ini struct:
> #comment
> [section]
> key1
> key2=
> key3= # this and earlier lines contains 0 values
> key4=a# 1 values
> key5=;# 1 empty values
> key6=a;f # 2 values
> key7= a ; f;; # 3 values
> [section2]
> ..
> But I don't know how implement endValue(without semicolon) and get lot of
> warnings from my grammer.
> This grammer return wrong parse tree([section2] as keyLine).
> I testet grammer in ANTLRWork 1.4.3
>
> grammar test;
> WS : (' '|'\t') {$channel=HIDDEN;};
> EOL : ('\r\n'|'\n'|'\r') ;
> SHARP : '#' {System.out.println("#");};
> EQUAL : '=' {System.out.println("=");};
> SEMICOLON : ';' {System.out.println(";");};
> COMMENT : SHARP .* EOL ;//{System.out.println("COM");};
> SECTION : '[' .* ']' {System.out.println("SEC");};
> ANY : . {System.out.println("ANY");};
> iniFile
> : section* EOF;
> section
> : commentLine* sectionLine COMMENT* (keyLine COMMENT?)*;
> commentLine
> : COMMENT;
> sectionLine
> : SECTION (EOL|COMMENT);
> keyLine
> : key keyValues* (EOL|COMMENT);
> key
> : ~('='|'#'|'['|EOL)+ {System.out.println("key");};
> keyValues
> : '=' (keyValue';')*;
> keyValue: ~(';'|'#'|EOL)* ;
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list