Hi,
I want to create a lexer rule to capture a C# attribute.
Example of C# attributes are:
[Serializable]
public class SampleClass
{
// Objects of this type can be serialized.
}
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
extern static void SampleMethod();
[Conditional(“DEBUG”), Conditional(“TEST1”)]
void TraceMethod()
{
// …
}
In the above examples, C# attributes are in bold. So, basically I want to capture all the things between “[” and “]” inclusively.
I have written a rule as below:
//Some declarations
let text_nondigit = ['A'-'Z' 'a'-'z' '_' '@' '$']
let digit = ['0'-'9']
let text= (text_nondigit) (text_nondigit|digit)*
| '[' text
{
let buf = (Buffer.create 32) in
let s = Stack.create () in
Stack.push '[' s;
Buffer.add_string buf ("["^text);
parse_csharp_annotation s buf lexbuf;
Buffer.contents buf;
}
And parse_csharp_annotation function is as below:
and parse_csharp_annotation s buf = parse
| ']' { Stack.pop s; Buffer.add_string buf "]"; parse_csharp_annotation s
buf lexbuf }
| '[' { Stack.push '[' s; Buffer.add_string buf "["; parse_csharp_annotation s buf lexbuf}
| eol as str { if not (Stack.is_empty s) then begin Buffer.add_string buf str;
update_line_number lexbuf; parse_csharp_annotation s buf lexbuf end
}
| _ {
Buffer.add_string buf (Lexing.lexeme lexbuf);
parse_csharp_annotation s buf lexbuf
}
Now, this works for me to some extent. But I also want to exclude an array index syntax from this capture rule.
For example, An array index can have same syntax as C# attribute like:
arrayName [indexVariable]
Do you have any suggestions for this problem?
Thank you in advance.