ocaml-lib / lexer.mll

{
open Tokens

let norm_string : string -> string =
  fun s ->
    let r = ref 0 in
    let w = ref 0 in
    let l = String.length s in
    while !r < l do
      let c = s.[!r] in
      if c = '\\'
      then begin
	incr r;
	match s.[!r] with
	| 'n' -> s.[!w] <- '\n'
	| 't' -> s.[!w] <- '\t'
	| '0'..'9' as c' ->
	    (try
	      s.[!w] <- Char.chr (int_of_string (String.sub s !r 3));
	      incr r;
	      incr r;
	    with _ ->
	      s.[!w] <- c')
	| c' -> s.[!w] <- c' end
      else s.[!w] <- c;
      incr r;
      incr w
    done;
    String.sub s 0 !w;;

} 

let skip_char = [' ' '\t' '\n']
let ident_char = ['A'-'Z' 'a'-'z' '0'-'9' '_']
let digit = ['0'-'9']
let special_char = ['~' '@' '#' '$' '%' '^' '&' '*' '-' '+' '=' '|' '\\' '/' '>' '<']

rule token = parse
| skip_char { token lexbuf }
| "\\\n" { token lexbuf }
| '`' { BackQuote }
| '!' { Exclam }
| '(' { LeftPar }
| ')' { RightPar }
| '{' { LeftAcc }
| '}' { RightAcc }
| '[' { LeftBra }
| ']' { RightBra }
| '?' { Interro }
| ',' { Comma }
| '.' { Dot }
| ':' { Colon }
| ';' { SemiColon }
| '"' { DoubleQuote }
| '\'' { Quote }
| '0'
    { Nat 0 }
| '-' '0'
    { MinusNat 0 }
| ['1'-'9'] digit*
    { Nat (int_of_string (Lexing.lexeme lexbuf)) }
| '-' ['1'-'9'] digit*
    { MinusNat (- (int_of_string (Lexing.lexeme lexbuf))) }
| ['A'-'Z' 'a'-'z' '_'] ident_char *
    { Symbol (Lexing.lexeme lexbuf) }
| special_char +
    { Symbol (Lexing.lexeme lexbuf) }
| '`' _ '`'
    { Char ((norm_string (Lexing.lexeme lexbuf)).[1]) }
| '"' [^ '\\' '"']* ('\\' _ [^ '\\' '"']*)* '"'
  { let s = Lexing.lexeme lexbuf in String (norm_string (String.sub s 1 (String.length s - 2))) }
| '\'' [^ '\\' '\'']* ('\\' _ [^ '\\' '\'']*)* '\''
  { let s = Lexing.lexeme lexbuf in Symbol (norm_string (String.sub s 1 (String.length s - 2))) }
| eof { raise Eof }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.