Pārlūkot izejas kodu

Fixed indenting in parser and lexer

Taddeus Kroes 12 gadi atpakaļ
vecāks
revīzija
71110a2f43
2 mainītis faili ar 215 papildinājumiem un 215 dzēšanām
  1. 78 78
      lexer.mll
  2. 137 137
      parser.mly

+ 78 - 78
lexer.mll

@@ -5,95 +5,95 @@ open Parser
 exception SyntaxError of string
 
 let next_line lexbuf =
-    let pos = lexbuf.lex_curr_p in
-    lexbuf.lex_curr_p <- {
-        pos with pos_bol = lexbuf.lex_curr_pos;
-                 pos_lnum = pos.pos_lnum + 1
-    }
+  let pos = lexbuf.lex_curr_p in
+  lexbuf.lex_curr_p <- {
+    pos with pos_bol = lexbuf.lex_curr_pos;
+             pos_lnum = pos.pos_lnum + 1
+  }
 }
 
 rule token = parse
-    | '#'' '+['0'-'9']+' '+'"'[^'"']+'"'(' '+['1'-'4'])*'\n' as marker {
-        (*
-         * The C preprocessor inserts so called ``line markers'' into the output.
-         * These markers have the following form:
-         *
-         *  # <linenum> <filename> <flags>
-         *
-         * This marker indicates that all lines after this marker up to the next
-         * marker come from the file <filename> starting at line <linenum>. After
-         * the file name can be zero or more flags, these flags are 1, 2, 3, 4.
-         * These flags can be ignored.
-         *)
-        let scan lnum fn =
-            let filename = String.sub fn 0 (String.length fn - 1) in
-            let pos = lexbuf.lex_curr_p in
-            lexbuf.lex_curr_p <- { pos with
-                                   pos_fname = filename;
-                                   pos_lnum = lnum;
-                                   pos_bol = pos.pos_cnum }
-        in
-        Scanf.sscanf marker "# %d \"%s" scan;
-        token lexbuf
-    }
+  | '#'' '+['0'-'9']+' '+'"'[^'"']+'"'(' '+['1'-'4'])*'\n' as marker {
+    (*
+     * The C preprocessor inserts so called ``line markers'' into the output.
+     * These markers have the following form:
+     *
+     *  # <linenum> <filename> <flags>
+     *
+     * This marker indicates that all lines after this marker up to the next
+     * marker come from the file <filename> starting at line <linenum>. After
+     * the file name can be zero or more flags, these flags are 1, 2, 3, 4.
+     * These flags can be ignored.
+     *)
+    let scan lnum fn =
+        let filename = String.sub fn 0 (String.length fn - 1) in
+        let pos = lexbuf.lex_curr_p in
+        lexbuf.lex_curr_p <- { pos with
+                               pos_fname = filename;
+                               pos_lnum = lnum;
+                               pos_bol = pos.pos_cnum }
+    in
+    Scanf.sscanf marker "# %d \"%s" scan;
+    token lexbuf
+  }
 
-    (*| ['('')''['']''{''}'';'','] as literal { literal }*)
+  (*| ['('')''['']''{''}'';'','] as literal { literal }*)
 
-    | '('       { LPAREN }
-    | ')'       { RPAREN }
-    | '['       { LBRACK }
-    | ']'       { RBRACK }
-    | '{'       { LBRACE }
-    | '}'       { RBRACE }
-    | ';'       { SEMICOL }
-    | ','       { COMMA }
+  | '('       { LPAREN }
+  | ')'       { RPAREN }
+  | '['       { LBRACK }
+  | ']'       { RBRACK }
+  | '{'       { LBRACE }
+  | '}'       { RBRACE }
+  | ';'       { SEMICOL }
+  | ','       { COMMA }
 
-    | '='       { ASSIGN }
-    | '!'       { NOT }
-    | '+'       { ADD }
-    | '-'       { SUB }
-    | '*'       { MUL }
-    | '/'       { DIV }
-    | '%'       { MOD }
+  | '='       { ASSIGN }
+  | '!'       { NOT }
+  | '+'       { ADD }
+  | '-'       { SUB }
+  | '*'       { MUL }
+  | '/'       { DIV }
+  | '%'       { MOD }
 
-    | "<="      { LE }
-    | "<"       { LT }
-    | ">="      { GE }
-    | ">"       { GT }
-    | "=="      { EQ }
-    | "!="      { NE }
-    | "&&"      { AND }
-    | "||"      { OR }
+  | "<="      { LE }
+  | "<"       { LT }
+  | ">="      { GE }
+  | ">"       { GT }
+  | "=="      { EQ }
+  | "!="      { NE }
+  | "&&"      { AND }
+  | "||"      { OR }
 
-    | "if"      { IF }
-    | "else"    { ELSE }
-    | "do"      { DO }
-    | "while"   { WHILE }
-    | "for"     { FOR }
-    | "return"  { RETURN }
-    | "extern"  { EXTERN }
-    | "export"  { EXPORT }
-    | "int"     { INT }
-    | "bool"    { BOOL }
-    | "float"   { FLOAT }
-    | "void"    { VOID }
+  | "if"      { IF }
+  | "else"    { ELSE }
+  | "do"      { DO }
+  | "while"   { WHILE }
+  | "for"     { FOR }
+  | "return"  { RETURN }
+  | "extern"  { EXTERN }
+  | "export"  { EXPORT }
+  | "int"     { INT }
+  | "bool"    { BOOL }
+  | "float"   { FLOAT }
+  | "void"    { VOID }
 
-    | "true"    { BOOL_CONST true }
-    | "false"   { BOOL_CONST false }
-    | ['0'-'9']+ as i              { INT_CONST (int_of_string i) }
-    | ['0'-'9']+'.'['0'-'9']+ as f { FLOAT_CONST (float_of_string f) }
-    | ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as id { ID id }
+  | "true"    { BOOL_CONST true }
+  | "false"   { BOOL_CONST false }
+  | ['0'-'9']+ as i              { INT_CONST (int_of_string i) }
+  | ['0'-'9']+'.'['0'-'9']+ as f { FLOAT_CONST (float_of_string f) }
+  | ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as id { ID id }
 
-    | '\r' | '\n' | "\r\n"  { next_line lexbuf; token lexbuf }
-    | [' ''\t']+            { token lexbuf }
-    | "//"[^'\n']*          { token lexbuf }
-    | "/*"                  { comment lexbuf }
-    | eof | '\000'          { EOF }
+  | '\r' | '\n' | "\r\n"  { next_line lexbuf; token lexbuf }
+  | [' ''\t']+            { token lexbuf }
+  | "//"[^'\n']*          { token lexbuf }
+  | "/*"                  { comment lexbuf }
+  | eof | '\000'          { EOF }
 
-    | _ as chr { raise (SyntaxError ("unexpected char: " ^ Char.escaped chr)) }
+  | _ as chr { raise (SyntaxError ("unexpected char: " ^ Char.escaped chr)) }
 
 (* Multi-line comments *)
 and comment = parse
-    | '\r' | '\n' | "\r\n"  { next_line lexbuf; comment lexbuf }
-    | "*/"                  { token lexbuf }
-    | _                     { comment lexbuf }
+  | '\r' | '\n' | "\r\n"  { next_line lexbuf; comment lexbuf }
+  | "*/"                  { token lexbuf }
+  | _                     { comment lexbuf }

+ 137 - 137
parser.mly

@@ -1,23 +1,23 @@
 %{
-    (**
-     * Parser for the CiviC language.
-     *
-     * Note that some shift/reduce conflicts exist, but these need not be solved
-     * since menhir can automatically resolve them
-     *)
+  (**
+   * Parser for the CiviC language.
+   *
+   * Note that some shift/reduce conflicts exist, but these need not be solved
+   * since menhir can automatically resolve them
+   *)
 
-    open Lexing
-    open Types
+  open Lexing
+  open Types
 
-    let loc start stop = [Loc (Util.loc_from_lexpos start stop)]
+  let loc start stop = [Loc (Util.loc_from_lexpos start stop)]
 
-    let rec make_dims dimloc = function
-        | [] -> []
-        | dim :: tail -> Dim (dim, dimloc) :: (make_dims dimloc tail)
+  let rec make_dims dimloc = function
+    | [] -> []
+    | dim :: tail -> Dim (dim, dimloc) :: (make_dims dimloc tail)
 
-    let rec make_args = function
-        | [] -> []
-        | h::t -> Arg h :: (make_args t)
+  let rec make_args = function
+    | [] -> []
+    | h::t -> Arg h :: (make_args t)
 %}
 
 (* Tokens *)
@@ -55,180 +55,180 @@
 
 (* Left-recursive list (use List.rev to obtain original list)  *)
 llist(x):
-    |            { [] }
-    | llist(x) x { $2 :: $1 }
+  |            { [] }
+  | llist(x) x { $2 :: $1 }
 
 (* Shorthand for comma-separated list *)
 %inline clist(x):
-    | lst=separated_list(COMMA, x)
-    { lst }
+  | lst=separated_list(COMMA, x)
+  { lst }
 
 basic_type:
-    | FLOAT { Float }
-    | INT   { Int }
-    | BOOL  { Bool }
+  | FLOAT { Float }
+  | INT   { Int }
+  | BOOL  { Bool }
 
 program:
-    | decl* EOF
-    { Program ($1, loc $startpos $endpos) }
+  | decl* EOF
+  { Program ($1, loc $startpos $endpos) }
 
 decl:
-    (* function: use location of function name *)
-    | EXTERN hdr=fun_header SEMICOL
-    { let (t, n, p, nameloc) = hdr in
-      FunDec(t, n, p, nameloc) }
+  (* function: use location of function name *)
+  | EXTERN hdr=fun_header SEMICOL
+  { let (t, n, p, nameloc) = hdr in
+    FunDec(t, n, p, nameloc) }
 
-    | export=boption(EXPORT) hdr=fun_header LBRACE body=fun_body RBRACE
-    { let (t, n, p, nameloc) = hdr in
-      FunDef (export, t, n, p, Block body, nameloc) }
+  | export=boption(EXPORT) hdr=fun_header LBRACE body=fun_body RBRACE
+  { let (t, n, p, nameloc) = hdr in
+    FunDef (export, t, n, p, Block body, nameloc) }
 
-    (* global variable declaration: use location of variable name *)
-    | EXTERN ctype=basic_type name=ID SEMICOL
-    { GlobalDec (ctype, name, loc $startpos $endpos) }
+  (* global variable declaration: use location of variable name *)
+  | EXTERN ctype=basic_type name=ID SEMICOL
+  { GlobalDec (ctype, name, loc $startpos $endpos) }
 
-    | EXTERN ctype=basic_type LBRACK dims=dimlist RBRACK name=ID SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      GlobalDec (ArrayDims (ctype, List.rev dims), name, loc) }
+  | EXTERN ctype=basic_type LBRACK dims=dimlist RBRACK name=ID SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    GlobalDec (ArrayDims (ctype, List.rev dims), name, loc) }
 
-    | export=boption(EXPORT) ctype=basic_type name=ID SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      GlobalDef (export, ctype, name, None, loc) }
+  | export=boption(EXPORT) ctype=basic_type name=ID SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    GlobalDef (export, ctype, name, None, loc) }
 
-    | export=boption(EXPORT) ctype=basic_type name=ID ASSIGN init=expr SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      GlobalDef (export, ctype, name, Some init, loc) }
+  | export=boption(EXPORT) ctype=basic_type name=ID ASSIGN init=expr SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    GlobalDef (export, ctype, name, Some init, loc) }
 
-    | export=boption(EXPORT) ctype=basic_type LBRACK dims=clist(expr) RBRACK
-      name=ID SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      GlobalDef (export, ArrayDims (ctype, dims), name, None, loc) }
+  | export=boption(EXPORT) ctype=basic_type LBRACK dims=clist(expr) RBRACK
+    name=ID SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    GlobalDef (export, ArrayDims (ctype, dims), name, None, loc) }
 
 fun_header:
-    (* function header: use location of function name *)
-    | ret=basic_type name=ID LPAREN params=clist(param) RPAREN
-    { (ret, name, params, loc $startpos(name) $endpos(name)) }
+  (* function header: use location of function name *)
+  | ret=basic_type name=ID LPAREN params=clist(param) RPAREN
+  { (ret, name, params, loc $startpos(name) $endpos(name)) }
 
-    | VOID name=ID LPAREN params=clist(param) RPAREN
-    { (Void, name, params, loc $startpos(name) $endpos(name)) }
+  | VOID name=ID LPAREN params=clist(param) RPAREN
+  { (Void, name, params, loc $startpos(name) $endpos(name)) }
 
 param:
-    (* parameter: use location of parameter name *)
-    | ctype=basic_type name=ID
-    { Param (ctype, name, loc $startpos(name) $endpos(name)) }
+  (* parameter: use location of parameter name *)
+  | ctype=basic_type name=ID
+  { Param (ctype, name, loc $startpos(name) $endpos(name)) }
 
-    | ctype=basic_type LBRACK dims=dimlist RBRACK name=ID
-    { let loc = loc $startpos(name) $endpos(name) in
-      Param (ArrayDims (ctype, List.rev dims), name, loc) }
+  | ctype=basic_type LBRACK dims=dimlist RBRACK name=ID
+  { let loc = loc $startpos(name) $endpos(name) in
+    Param (ArrayDims (ctype, List.rev dims), name, loc) }
 
 dimlist:
-    | name=ID
-    { [Dim (name, loc $startpos(name) $endpos(name))] }
-    | head=dimlist COMMA name=ID
-    { Dim (name, loc $startpos(name) $endpos(name)) :: head }
+  | name=ID
+  { [Dim (name, loc $startpos(name) $endpos(name))] }
+  | head=dimlist COMMA name=ID
+  { Dim (name, loc $startpos(name) $endpos(name)) :: head }
 
 fun_body:
-    | llist(var_dec) local_fun_dec* statement* loption(return_statement)
-    { VarDecs (List.rev $1) :: (LocalFuns $2) :: $3 @ $4 }
+  | llist(var_dec) local_fun_dec* statement* loption(return_statement)
+  { VarDecs (List.rev $1) :: (LocalFuns $2) :: $3 @ $4 }
 
 return_statement:
-    (* return statement: use location of return value *)
-    | RETURN value=expr SEMICOL
-    { [Return (value, loc $startpos(value) $endpos(value))] }
+  (* return statement: use location of return value *)
+  | RETURN value=expr SEMICOL
+  { [Return (value, loc $startpos(value) $endpos(value))] }
 
 (* function: use location of function name *)
 local_fun_dec:
-    | hdr=fun_header LBRACE body=fun_body RBRACE
-    { let (t, n, p, nameloc) = hdr in
-      FunDef (false, t, n, p, Block body, nameloc) }
+  | hdr=fun_header LBRACE body=fun_body RBRACE
+  { let (t, n, p, nameloc) = hdr in
+    FunDef (false, t, n, p, Block body, nameloc) }
 
 var_dec:
-    (* variable declaration: use location of variable name *)
-    | ctype=basic_type name=ID SEMICOL
-    { VarDec (ctype, name, None, loc $startpos(name) $endpos(name)) }
+  (* variable declaration: use location of variable name *)
+  | ctype=basic_type name=ID SEMICOL
+  { VarDec (ctype, name, None, loc $startpos(name) $endpos(name)) }
 
-    | ctype=basic_type name=ID ASSIGN init=expr SEMICOL
-    { VarDec (ctype, name, Some init, loc $startpos(name) $endpos(name)) }
+  | ctype=basic_type name=ID ASSIGN init=expr SEMICOL
+  { VarDec (ctype, name, Some init, loc $startpos(name) $endpos(name)) }
 
-    | ctype=basic_type LBRACK dims=clist(expr) RBRACK name=ID SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      VarDec (ArrayDims (ctype, dims), name, None, loc) }
+  | ctype=basic_type LBRACK dims=clist(expr) RBRACK name=ID SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    VarDec (ArrayDims (ctype, dims), name, None, loc) }
 
-    | ctype=basic_type LBRACK dims=clist(expr) RBRACK name=ID ASSIGN
-      init=expr SEMICOL
-    { let loc = loc $startpos(name) $endpos(name) in
-      VarDec (ArrayDims (ctype, dims), name, Some init, loc) }
+  | ctype=basic_type LBRACK dims=clist(expr) RBRACK name=ID ASSIGN
+    init=expr SEMICOL
+  { let loc = loc $startpos(name) $endpos(name) in
+    VarDec (ArrayDims (ctype, dims), name, Some init, loc) }
 
 statement:
-    (* assignment: use location of assigned variable name *)
-    | name=ID ASSIGN value=expr SEMICOL
-    { Assign (name, None, value, loc $startpos(name) $endpos(name)) }
+  (* assignment: use location of assigned variable name *)
+  | name=ID ASSIGN value=expr SEMICOL
+  { Assign (name, None, value, loc $startpos(name) $endpos(name)) }
 
-    | name=ID LBRACK dims=clist(expr) brk=RBRACK ASSIGN value=expr SEMICOL
-    { Assign (name, Some dims, value, loc $startpos(name) $endpos(brk)) }
+  | name=ID LBRACK dims=clist(expr) brk=RBRACK ASSIGN value=expr SEMICOL
+  { Assign (name, Some dims, value, loc $startpos(name) $endpos(brk)) }
 
-    | name=ID LPAREN args=clist(expr) RPAREN SEMICOL
-    { Expr (FunCall (name, make_args args, loc $startpos(name) $endpos(name))) }
+  | name=ID LPAREN args=clist(expr) RPAREN SEMICOL
+  { Expr (FunCall (name, make_args args, loc $startpos(name) $endpos(name))) }
 
-    (* if-statements and (do-)while-loops: use location of condition *)
-    | IF LPAREN cond=expr RPAREN body=block
-    { If (cond, Block body, loc $startpos $endpos) }                %prec IF
+  (* if-statements and (do-)while-loops: use location of condition *)
+  | IF LPAREN cond=expr RPAREN body=block
+  { If (cond, Block body, loc $startpos $endpos) }                %prec IF
 
-    | IF LPAREN c=expr RPAREN t=block ELSE f=block
-    { IfElse (c, Block t, Block f, loc $startpos(c) $endpos(c)) }   %prec ELSE
+  | IF LPAREN c=expr RPAREN t=block ELSE f=block
+  { IfElse (c, Block t, Block f, loc $startpos(c) $endpos(c)) }   %prec ELSE
 
-    | WHILE LPAREN cond=expr RPAREN body=block
-    { While (cond, Block body, loc $startpos(cond) $endpos(cond)) }
+  | WHILE LPAREN cond=expr RPAREN body=block
+  { While (cond, Block body, loc $startpos(cond) $endpos(cond)) }
 
-    | DO body=block WHILE LPAREN cond=expr RPAREN SEMICOL
-    { DoWhile (cond, Block body, loc $startpos(cond) $endpos(cond)) }
+  | DO body=block WHILE LPAREN cond=expr RPAREN SEMICOL
+  { DoWhile (cond, Block body, loc $startpos(cond) $endpos(cond)) }
 
-    (* for-loop: use location of counter id *)
-    | FOR LPAREN INT cnt=ID ASSIGN start=expr COMMA stop=expr RPAREN body=block
-    { let loc = loc $startpos(cnt) $endpos(cnt) in
-        For (cnt, start, stop, Const (IntVal 1, []), Block body, loc) }
+  (* for-loop: use location of counter id *)
+  | FOR LPAREN INT cnt=ID ASSIGN start=expr COMMA stop=expr RPAREN body=block
+  { let loc = loc $startpos(cnt) $endpos(cnt) in
+    For (cnt, start, stop, Const (IntVal 1, []), Block body, loc) }
 
-    | FOR LPAREN INT cnt=ID ASSIGN start=expr COMMA stop=expr COMMA step=expr
-      RPAREN body=block
-    { let loc = loc $startpos(cnt) $endpos(cnt) in
-      For (cnt, start, stop, step, Block body, loc) }
+  | FOR LPAREN INT cnt=ID ASSIGN start=expr COMMA stop=expr COMMA step=expr
+    RPAREN body=block
+  { let loc = loc $startpos(cnt) $endpos(cnt) in
+    For (cnt, start, stop, step, Block body, loc) }
 
 block:
-    | LBRACE stats=statement* RBRACE  { stats }
-    | stat=statement                  { [stat] }
+  | LBRACE stats=statement* RBRACE  { stats }
+  | stat=statement                  { [stat] }
 
 expr:
-    | name=ID LPAREN args=clist(expr) RPAREN
-    { FunCall (name, make_args args, loc $startpos $endpos) }
-
-    | LPAREN expr RPAREN              { $2 }
-    | ID                              { Var ($1, None, loc $startpos $endpos) }
-    | l=expr op=binop r=expr          { Binop (op, l, r, loc $startpos $endpos) }
-    | SUB expr                        { Monop (Neg, $2, loc $startpos $endpos) }    %prec NEG
-    | NOT expr                        { Monop (Not, $2, loc $startpos $endpos) }
-    | LPAREN basic_type RPAREN expr   { TypeCast ($2, $4, loc $startpos $endpos) }  %prec CAST
-    | FLOAT_CONST                     { Const (FloatVal $1, loc $startpos $endpos) }
-    | INT_CONST                       { Const (IntVal $1, loc $startpos $endpos) }
-    | BOOL_CONST                      { Const (BoolVal $1, loc $startpos $endpos) }
-    | ID array_const                  { Var ($1, Some $2, loc $startpos $endpos) }
-    | array_const                     { ArrayConst ($1, loc $startpos $endpos) }
+  | name=ID LPAREN args=clist(expr) RPAREN
+  { FunCall (name, make_args args, loc $startpos $endpos) }
+
+  | LPAREN expr RPAREN              { $2 }
+  | ID                              { Var ($1, None, loc $startpos $endpos) }
+  | l=expr op=binop r=expr          { Binop (op, l, r, loc $startpos $endpos) }
+  | SUB expr                        { Monop (Neg, $2, loc $startpos $endpos) }    %prec NEG
+  | NOT expr                        { Monop (Not, $2, loc $startpos $endpos) }
+  | LPAREN basic_type RPAREN expr   { TypeCast ($2, $4, loc $startpos $endpos) }  %prec CAST
+  | FLOAT_CONST                     { Const (FloatVal $1, loc $startpos $endpos) }
+  | INT_CONST                       { Const (IntVal $1, loc $startpos $endpos) }
+  | BOOL_CONST                      { Const (BoolVal $1, loc $startpos $endpos) }
+  | ID array_const                  { Var ($1, Some $2, loc $startpos $endpos) }
+  | array_const                     { ArrayConst ($1, loc $startpos $endpos) }
 
 %inline binop:
-    | ADD  { Add }
-    | SUB  { Sub }
-    | MUL  { Mul }
-    | DIV  { Div }
-    | MOD  { Mod }
-    | EQ   { Eq  }
-    | NE   { Ne  }
-    | LT   { Lt  }
-    | LE   { Le  }
-    | GT   { Gt  }
-    | GE   { Ge  }
-    | AND  { And }
-    | OR   { Or  }
+  | ADD  { Add }
+  | SUB  { Sub }
+  | MUL  { Mul }
+  | DIV  { Div }
+  | MOD  { Mod }
+  | EQ   { Eq  }
+  | NE   { Ne  }
+  | LT   { Lt  }
+  | LE   { Le  }
+  | GT   { Gt  }
+  | GE   { Ge  }
+  | AND  { And }
+  | OR   { Or  }
 
 array_const:
-    LBRACK values=clist(expr) RBRACK
-    { values }
+  | LBRACK values=clist(expr) RBRACK
+  { values }
 
 %%