## lustrec / src / lexer_prelude.mll @ 37419cf4

History | View | Annotate | Download (3.21 KB)

1 | a2d97a3e | ploc | (********************************************************************) |
---|---|---|---|

2 | (* *) |
||

3 | (* The LustreC compiler toolset / The LustreC Development Team *) |
||

4 | (* Copyright 2012 - -- ONERA - CNRS - INPT *) |
||

5 | (* *) |
||

6 | (* LustreC is free software, distributed WITHOUT ANY WARRANTY *) |
||

7 | (* under the terms of the GNU Lesser General Public License *) |
||

8 | (* version 2.1. *) |
||

9 | (* *) |
||

10 | (********************************************************************) |
||

11 | 22fe1c93 | ploc | |

12 | { |
||

13 | open Parser_prelude |
||

14 | open Utils |
||

15 | |||

16 | exception Error of Location.t |
||

17 | |||

18 | (* As advised by Caml documentation. This way a single lexer rule is |
||

19 | used to handle all the possible keywords. *) |
||

20 | let keyword_table = |
||

21 | create_hashtable 20 [ |
||

22 | "true", TRUE; |
||

23 | "false", FALSE; |
||

24 | "stateless", STATELESS; |
||

25 | "if", IF; |
||

26 | "then", THEN; |
||

27 | "else", ELSE; |
||

28 | "merge", MERGE; |
||

29 | "arrow", ARROW; |
||

30 | "fby", FBY; |
||

31 | "when", WHEN; |
||

32 | "whennot", WHENNOT; |
||

33 | "every", EVERY; |
||

34 | "node", NODE; |
||

35 | "sensor", SENSOR; |
||

36 | "actuator", ACTUATOR; |
||

37 | "let", LET; |
||

38 | "tel", TEL; |
||

39 | "returns", RETURNS; |
||

40 | "var", VAR; |
||

41 | "imported", IMPORTED; |
||

42 | "wcet", WCET; |
||

43 | "int", TINT; |
||

44 | "bool", TBOOL; |
||

45 | "float", TFLOAT; |
||

46 | "real", TREAL; |
||

47 | "clock", TCLOCK; |
||

48 | "rate", RATE; |
||

49 | "due", DUE; |
||

50 | "not", NOT; |
||

51 | "tail", TAIL; |
||

52 | "and", AND; |
||

53 | "or", OR; |
||

54 | "xor", OR; |
||

55 | "mod", MOD; |
||

56 | "pre", PRE; |
||

57 | "div", DIV; |
||

58 | "const", CONST; |
||

59 | "include", INCLUDE |
||

60 | ] |
||

61 | |||

62 | (* Update line number for location info *) |
||

63 | let incr_line lexbuf = |
||

64 | let pos = lexbuf.Lexing.lex_curr_p in |
||

65 | lexbuf.Lexing.lex_curr_p <- { pos with |
||

66 | Lexing.pos_lnum = pos.Lexing.pos_lnum + 1; |
||

67 | Lexing.pos_bol = pos.Lexing.pos_cnum; |
||

68 | } |
||

69 | } |
||

70 | |||

71 | let newline = ('\010' | '\013' | "\013\010") |
||

72 | let notnewline = [^ '\010' '\013'] |
||

73 | let blank = [' ' '\009' '\012'] |
||

74 | |||

75 | rule token = parse |
||

76 | | "/*" |
||

77 | { comment 0 lexbuf } |
||

78 | | "--" notnewline* (newline|eof) |
||

79 | { incr_line lexbuf; |
||

80 | token lexbuf } |
||

81 | | newline |
||

82 | { incr_line lexbuf; |
||

83 | token lexbuf } |
||

84 | | blank + |
||

85 | {token lexbuf} |
||

86 | | ['0'-'9'] ['0'-'9']* '.' ['0'-'9']* |
||

87 | {FLOAT (float_of_string (Lexing.lexeme lexbuf))} |
||

88 | | ['0'-'9']+ |
||

89 | {INT (int_of_string (Lexing.lexeme lexbuf)) } |
||

90 | | ['0'-'9']+ '.' ['0'-'9']+ 'E' ('+'|'-') ['0'-'9'] ['0'-'9'] as s {REAL s} |
||

91 | | "tel." {TEL} |
||

92 | | "tel;" {TEL} |
||

93 | | ['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']* |
||

94 | {let s = Lexing.lexeme lexbuf in |
||

95 | try |
||

96 | Hashtbl.find keyword_table s |
||

97 | with Not_found -> |
||

98 | IDENT s} |
||

99 | | "->" {ARROW} |
||

100 | | "=>" {IMPL} |
||

101 | | "<=" {LTE} |
||

102 | | ">=" {GTE} |
||

103 | | "<>" {NEQ} |
||

104 | | '<' {LT} |
||

105 | | '>' {GT} |
||

106 | | "!=" {NEQ} |
||

107 | | '-' {MINUS} |
||

108 | | '+' {PLUS} |
||

109 | | '/' {DIV} |
||

110 | | '*' {MULT} |
||

111 | | '=' {EQ} |
||

112 | | '(' {LPAR} |
||

113 | | ')' {RPAR} |
||

114 | | ';' {SCOL} |
||

115 | | ':' {COL} |
||

116 | | ',' {COMMA} |
||

117 | | '=' {EQ} |
||

118 | | '/' {DIV} |
||

119 | | "&&" {AMPERAMPER} |
||

120 | | "||" {BARBAR} |
||

121 | | "*^" {UCLOCK} |
||

122 | | "/^" {DCLOCK} |
||

123 | | "~>" {PHCLOCK} |
||

124 | | "::" {COLCOL} |
||

125 | | "^" {POWER} |
||

126 | | '"' {QUOTE} |
||

127 | | eof { EOF } |
||

128 | | _ { raise (Error (Location.curr lexbuf)) } |
||

129 | and comment n = parse |
||

130 | | eof |
||

131 | { raise (Error (Location.curr lexbuf)) } |
||

132 | | "/*" |
||

133 | { comment (n+1) lexbuf } |
||

134 | | "*/" |
||

135 | { if n > 0 then comment (n-1) lexbuf else token lexbuf } |
||

136 | | newline |
||

137 | { incr_line lexbuf; |
||

138 | comment n lexbuf } |
||

139 | | _ { comment n lexbuf } |