"Fossies" - the Fresh Open Source Software Archive

Member "fsharp-4.5/src/absil/illex.fsl" (5 Sep 2018, 6099 Bytes) of package /linux/misc/mono-sources/fsharp/fsharp-4.5.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "illex.fsl": 4.1.33_vs_4.5.

    1 // Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
    2 
    3 {
    4 
    5 module internal Microsoft.FSharp.Compiler.AbstractIL.Internal.AsciiLexer 
    6 
    7 open Internal.Utilities
    8 open Internal.Utilities.Collections
    9 open Internal.Utilities.Text
   10 open Internal.Utilities.Text.Lexing
   11 open Microsoft.FSharp.Compiler.AbstractIL 
   12 open Microsoft.FSharp.Compiler.AbstractIL.Internal 
   13 open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
   14 
   15 open Microsoft.FSharp.Compiler.AbstractIL.Internal.AsciiParser 
   16 open Microsoft.FSharp.Compiler.AbstractIL.Internal.AsciiConstants 
   17 
   18 
   19 let lexeme (lexbuf : LexBuffer<char>) = new System.String(lexbuf.Lexeme)
   20 
   21 let unexpectedChar _lexbuf =
   22   raise Parsing.RecoverableParseError ;;
   23 
   24 // -------------------------------------------------------------------- 
   25 // Keywords
   26 // -------------------------------------------------------------------- 
   27 
   28 let keywords = 
   29     lazy [ "void",VOID; 
   30            "bool",BOOL;
   31            "bytearray",BYTEARRAY;
   32            "char",CHAR;
   33            "class",CLASS;
   34            "default",DEFAULT;
   35            "explicit",EXPLICIT;
   36            "float32",FLOAT32;
   37            "float64",FLOAT64;
   38            "instance",INSTANCE;
   39            "int",INT;
   40            "int16",INT16;
   41            "int32",INT32;
   42            "int64",INT64;
   43            "int8",INT8;
   44            "method",METHOD;
   45            "native",NATIVE;
   46            "object", OBJECT;
   47            "string",STRING;
   48            "uint",UINT;
   49            "uint16",UINT16;
   50            "uint32",UINT32;
   51            "uint64",UINT64;
   52            "uint8",UINT8;
   53            "unmanaged",UNMANAGED;
   54            "unsigned",UNSIGNED;
   55            "value",VALUE;
   56            "valuetype",VALUETYPE;
   57            "vararg",VARARG ] 
   58 
   59 // -------------------------------------------------------------------- 
   60 // Instructions
   61 // -------------------------------------------------------------------- 
   62 
   63 let kwdInstrTable = 
   64   lazy 
   65    (let t = HashMultiMap(1000, HashIdentity.Structural)
   66     List.iter t.Add (Lazy.force keywords);
   67     let addTable f l = List.iter (fun (x,i) -> t.Add (String.concat "." x,f i)) (Lazy.force l)
   68     addTable (fun i -> INSTR_NONE i) NoArgInstrs;
   69     addTable (fun i -> INSTR_I i) Int32Instrs;
   70     addTable (fun i -> INSTR_I32_I32 i) Int32Int32Instrs;
   71     addTable (fun i -> INSTR_I8 i) Int64Instrs;
   72     addTable (fun i -> INSTR_R i) DoubleInstrs;
   73     addTable (fun i -> INSTR_METHOD i) MethodSpecInstrs;
   74     addTable (fun i -> INSTR_TYPE i) TypeInstrs;
   75     addTable (fun i -> INSTR_INT_TYPE i) IntTypeInstrs;
   76     addTable (fun i -> INSTR_VALUETYPE i) ValueTypeInstrs;
   77     addTable (fun i -> INSTR_STRING i) StringInstrs;
   78     addTable (fun i -> INSTR_TOK i) TokenInstrs;
   79     t)
   80 
   81 let kwdOrInstr s = (Lazy.force kwdInstrTable).[s] (* words *)
   82 
   83 let eval = function 
   84   | '0' -> 0  | '1' -> 1 | '2' -> 2  | '3' -> 3  | '4' -> 4  | '5' -> 5 
   85   | '6' -> 6  | '7' -> 7  | '8' -> 8  | '9' -> 9 
   86   | 'A' -> 10 | 'B' -> 11 | 'C' -> 12 | 'D' -> 13 | 'E' -> 14 | 'F' -> 15
   87   | 'a' -> 10 | 'b' -> 11 | 'c' -> 12 | 'd' -> 13 | 'e' -> 14 | 'f' -> 15
   88   | _ -> failwith "bad hexbyte"  
   89 
   90 let kwdOrInstrOrId s = match (Lazy.force kwdInstrTable).TryFind s with Some v -> v | _ -> VAL_ID s
   91         
   92 }
   93 
   94 // -------------------------------------------------------------------- 
   95 // The Rules
   96 // -------------------------------------------------------------------- 
   97 rule token = parse
   98   | "," { COMMA }
   99   | "." { DOT }
  100   | "*" { STAR }
  101   | "!" { BANG }
  102   | "&" { AMP }
  103   | "(" { LPAREN }
  104   | ")" { RPAREN }
  105   | "[" { LBRACK }
  106   | "]" { RBRACK }
  107   | "/" { SLASH }
  108   | "<" { LESS }
  109   | ">" { GREATER }
  110   | "..." { ELIPSES }
  111   | "::" { DCOLON }
  112   | "+" { PLUS }
  113   | (['0'-'9']) | (['0'-'9']['0'-'9']['0'-'9']+)
  114       {  VAL_INT64(int64(lexeme lexbuf)) }
  115 
  116   (* We need to be able to parse all of *)
  117   (* ldc.r8     0. *)
  118   (* float64(-657435.)     *)
  119   (* and int32[0...,0...] *)
  120   (* The problem is telling an integer-followed-by-ellipses from a floating-point-nubmer-followed-by-dots *)
  121 
  122   | ((['0'-'9']) | (['0'-'9']['0'-'9']['0'-'9']+)) "..."
  123       {  let b = lexeme lexbuf in 
  124          VAL_INT32_ELIPSES(int32(String.sub b  0 (String.length b - 3))) }
  125   | ['0'-'9' 'A'-'F' 'a'-'f' ] ['0'-'9' 'A'-'F' 'a'-'f' ] 
  126       { let c1 = String.get (lexeme lexbuf) 0 in 
  127         let c2 = String.get (lexeme lexbuf) 1 in 
  128         if c1 >= '0' && c1 <= '9' && c2 >= '0' && c2 <= '9' then 
  129           VAL_INT64(int64 (10*eval c1 + eval c2) )
  130         else VAL_ID(lexeme lexbuf) }
  131   | '0' 'x' ['0'-'9' 'a'-'f' 'A'-'F']+ 
  132       { VAL_INT64(int64(lexeme lexbuf)) }
  133   | "FFFFFF"  ['0'-'9' 'A'-'F' 'a'-'f' ] ['0'-'9' 'A'-'F' 'a'-'f' ] 
  134       { let c1 = (lexeme lexbuf).[6] in 
  135         let c2 = (lexeme lexbuf).[7] in 
  136         if c1 >= '0' && c1 <= '9' && c2 >= '0' && c2 <= '9' then 
  137           VAL_INT64(int64 (10*eval c1 + eval c2)) 
  138         else VAL_ID(lexeme lexbuf) }
  139 
  140   | '-' ['0'-'9']+ 
  141       { VAL_INT64(int64(lexeme lexbuf)) }
  142   | ('+'|'-')? ['0'-'9']+ ('.' ['0' - '9']*)? (('E'|'e') ('-'|'+')? ['0' - '9']+)?
  143       { VAL_FLOAT64( (float (lexeme lexbuf)) ) }
  144 
  145   | ("conv"|"cgt"|"clt"|"ceq"|"add"|"sub"|"div"|"rem"|"mul"|"beq"|"bne"|"cne"|"ldarga"|"ldloca"|"ldind"|"newarr"|"shr"|"starg"|"stind"|"ldelem"|"ldelema"|"ldlen"|"stelem"|"unbox"|"box"|"initobj") '.' ['a'-'z' 'A'-'Z' '0'-'9' '.']+
  146       { let s = (lexeme lexbuf) in kwdOrInstr s }
  147   | [ '`'  '\128'-'\255' '@' '?' '$' 'a'-'z' 'A'-'Z' '_'] [  '`' '\128'-'\255' '$' 'a'-'z' 'A'-'Z' '0'-'9' '-' '_' '@' '$' ] *
  148       { kwdOrInstrOrId (lexeme lexbuf) }
  149   | [ '`'  '\128'-'\255' '@' '?' '$' 'a'-'z' 'A'-'Z' '_'] [  '`' '\128'-'\255' '$' 'a'-'z' 'A'-'Z' '0'-'9' '-' '_' '@' '$' ]+
  150         ('.' [ '`'  '\128'-'\255' '@' '?' '$' 'a'-'z' 'A'-'Z' '_'] [  '`' '\128'-'\255' '$' 'a'-'z' 'A'-'Z' '0'-'9' '-' '_' '@' '$' ] +)+
  151       { VAL_DOTTEDNAME(lexeme lexbuf) } 
  152                                             
  153   |   [' ' '\t' '\r' '\n']                  
  154       { token lexbuf }                      
  155   | _ 
  156       { unexpectedChar lexbuf }            
  157   | eof                                     
  158       { EOF }                                     
  159