<commit_before>%{
(** A JavaScript parser that does not do semicolon insertion. *)
open Prelude
open JavaScript_syntax

exception Expected_lvalue

exception Parse_failure of string

let rec expr_to_lvalue (e : expr) : lvalue =  match e with
    VarExpr (p,x) -> VarLValue (p,x)
  | DotExpr (p,e,x) -> DotLValue (p,e,x)
  | BracketExpr (p,e1,e2) -> BracketLValue (p,e1,e2)
  | ParenExpr (_, e) -> expr_to_lvalue e
  | _ -> raise Expected_lvalue
%}

%token <string> ContinueId
%token <string> BreakId
%token <Prelude.pos * string> Id
%token <Prelude.pos * string> String
%token <string * bool * bool> Regexp
%token <Prelude.pos * int> Int
%token <Prelude.pos * float> Float
%token <JavaScript_syntax.assignOp> AssignOp

%token If Else True False New Instanceof This Null Function Typeof Void
 Delete Switch Default Case While Do Break Var In For Try Catch Finally Throw
 Return With Continue 

%token LBrace RBrace LParen RParen Assign
 Semi Comma Ques Colon LOr LAnd BOr BXor BAnd StrictEq AbstractEq
 StrictNEq AbstractNEq LShift RShift SpRShift LEq LT GEq GT PlusPlus MinusMinus
 Plus Minus Times Div Mod Exclamation Tilde Period LBrack RBrack

%token EOF


/* http://stackoverflow.com/questions/1737460/
   how-to-find-shift-reduce-conflict-in-this-yacc-file */
%nonassoc GreaterThanColon
%nonassoc Colon
%nonassoc LowerThanElse
%nonassoc Else

%left LOr
%left LAnd
%left BOr
%left BXor
%left BAnd
%left StrictEq StrictNEq AbstractEq AbstractNEq
%left LT LEq GT GEq In Instanceof
%left LShift RShift SpRShift
%left Plus Minus
%left Times Div Mod


%start program
%start expression

%type <JavaScript_syntax.prog> program
%type <JavaScript_syntax.expr> expression

%%

exprs
  : { [] }
  | assign_expr { [$1] }
  | assign_expr Comma exprs { $1::$3 }

stmts
  : { [] }
  | stmt stmts { $1 :: $2 }

cases
  : { [] }
  | case cases { $1 :: $2 }

catches
  : { [] }
  | catch catches { $1 :: $2 }


ids
  : { [] }
  | Id { let _,x = $1 in [x] }
  | Id Comma ids { let _,x = $1 in x :: $3 }

prop
  : Id { let _,x = $1 in PropId x }  %prec GreaterThanColon
  | String { let _,s = $1 in  PropString s }

fields
  : { [] }
  | prop Colon expr 
    { [ (($startpos($1), $startpos($3)), $1, $3) ] }
  | prop Colon expr Comma fields  
      { (($startpos($1), $startpos($3)), $1, $3) :: $5 }

varDecls
  : varDecl { [$1] }
  | varDecl Comma varDecls { $1::$3 } 

varDecls_noin
  : varDecl_noin { [$1] }
  | varDecl_noin Comma varDecls_noin { $1::$3 } 


/*******************************************************************************
 * Expressions
 */

element_list
  : 
      { [] }
  | Comma 
      { [UndefinedExpr (( $startpos, $endpos))] }
  | assign_expr { [$1] }
  | assign_expr Comma element_list 
      { $1::$3 }

primary_expr
  : True 
      { BoolExpr (($startpos, $endpos), true) }
  | False 
      { BoolExpr (($startpos, $endpos), false) }
  | This 
      { ThisExpr (($startpos, $endpos)) }
  | Null
      { NullExpr (($startpos, $endpos)) }  
  | Id 
      { let loc,x = $1 in VarExpr (loc,x) }
  | LBrack element_list RBrack
      { ArrayExpr (($startpos, $endpos),$2) }
  /* shift/reduce conflict with labelled statements when the next token
      is Colon. We favor shift: if we see a colon, we assume we're starting
      a labelled statement, not an object. */
  | LBrace fields RBrace
      { ObjectExpr (($startpos, $endpos),$2) }
  | String
      { let loc, s = $1 in StringExpr (loc, s) }
  | Regexp
      { let re,g,ci = $1 in 
          RegexpExpr 
            (($startpos, $endpos), re, g, ci) }
  | Int
      { let loc,n = $1 in IntExpr (loc,n) }
  | Float
      { let loc,f = $1 in NumExpr (loc,f) }
  | LParen expr RParen
      { ParenExpr (($startpos, $endpos),$2) }

member_expr
  : primary_expr 
      { $1 }
  | Function LParen ids RParen LBrace src_elts RBrace
    { FuncExpr (($startpos, $endpos), $3, 
                BlockStmt (($startpos($5), $endpos($7)), $6)) }
/* Reduce/reduce conflict with function statements.  Who here knew that
   named function expressions existed?  
  | Function Id LParen ids RParen LBrace src_elts RBrace
    { let _,x = $2 in NamedFuncExpr ($1, x, $4, BlockStmt ($6, $7)) }
*/
  | member_expr Period Id 
      { let _,x = $3 in DotExpr (($startpos, $endpos),$1,x) } <commit_msg>removed old comment<commit_after>%{
(** A JavaScript parser that does not do semicolon insertion. *)
open Prelude
open JavaScript_syntax

exception Expected_lvalue

exception Parse_failure of string

let rec expr_to_lvalue (e : expr) : lvalue =  match e with
    VarExpr (p,x) -> VarLValue (p,x)
  | DotExpr (p,e,x) -> DotLValue (p,e,x)
  | BracketExpr (p,e1,e2) -> BracketLValue (p,e1,e2)
  | ParenExpr (_, e) -> expr_to_lvalue e
  | _ -> raise Expected_lvalue
%}

%token <string> ContinueId
%token <string> BreakId
%token <Prelude.pos * string> Id
%token <Prelude.pos * string> String
%token <string * bool * bool> Regexp
%token <Prelude.pos * int> Int
%token <Prelude.pos * float> Float
%token <JavaScript_syntax.assignOp> AssignOp

%token If Else True False New Instanceof This Null Function Typeof Void
 Delete Switch Default Case While Do Break Var In For Try Catch Finally Throw
 Return With Continue 

%token LBrace RBrace LParen RParen Assign
 Semi Comma Ques Colon LOr LAnd BOr BXor BAnd StrictEq AbstractEq
 StrictNEq AbstractNEq LShift RShift SpRShift LEq LT GEq GT PlusPlus MinusMinus
 Plus Minus Times Div Mod Exclamation Tilde Period LBrack RBrack

%token EOF


/* http://stackoverflow.com/questions/1737460/
   how-to-find-shift-reduce-conflict-in-this-yacc-file */
%nonassoc GreaterThanColon
%nonassoc Colon
%nonassoc LowerThanElse
%nonassoc Else

%left LOr
%left LAnd
%left BOr
%left BXor
%left BAnd
%left StrictEq StrictNEq AbstractEq AbstractNEq
%left LT LEq GT GEq In Instanceof
%left LShift RShift SpRShift
%left Plus Minus
%left Times Div Mod


%start program
%start expression

%type <JavaScript_syntax.prog> program
%type <JavaScript_syntax.expr> expression

%%

exprs
  : { [] }
  | assign_expr { [$1] }
  | assign_expr Comma exprs { $1::$3 }

stmts
  : { [] }
  | stmt stmts { $1 :: $2 }

cases
  : { [] }
  | case cases { $1 :: $2 }

catches
  : { [] }
  | catch catches { $1 :: $2 }


ids
  : { [] }
  | Id { let _,x = $1 in [x] }
  | Id Comma ids { let _,x = $1 in x :: $3 }

prop
  : Id { let _,x = $1 in PropId x }  %prec GreaterThanColon
  | String { let _,s = $1 in  PropString s }

fields
  : { [] }
  | prop Colon expr 
    { [ (($startpos($1), $startpos($3)), $1, $3) ] }
  | prop Colon expr Comma fields  
      { (($startpos($1), $startpos($3)), $1, $3) :: $5 }

varDecls
  : varDecl { [$1] }
  | varDecl Comma varDecls { $1::$3 } 

varDecls_noin
  : varDecl_noin { [$1] }
  | varDecl_noin Comma varDecls_noin { $1::$3 } 


/*******************************************************************************
 * Expressions
 */

element_list
  : 
      { [] }
  | Comma 
      { [UndefinedExpr (( $startpos, $endpos))] }
  | assign_expr { [$1] }
  | assign_expr Comma element_list 
      { $1::$3 }

primary_expr
  : True 
      { BoolExpr (($startpos, $endpos), true) }
  | False 
      { BoolExpr (($startpos, $endpos), false) }
  | This 
      { ThisExpr (($startpos, $endpos)) }
  | Null
      { NullExpr (($startpos, $endpos)) }  
  | Id 
      { let loc,x = $1 in VarExpr (loc,x) }
  | LBrack element_list RBrack
      { ArrayExpr (($startpos, $endpos),$2) }
  | LBrace fields RBrace
      { ObjectExpr (($startpos, $endpos),$2) }
  | String
      { let loc, s = $1 in StringExpr (loc, s) }
  | Regexp
      { let re,g,ci = $1 in 
          RegexpExpr 
            (($startpos, $endpos), re, g, ci) }
  | Int
      { let loc,n = $1 in IntExpr (loc,n) }
  | Float
      { let loc,f = $1 in NumExpr (loc,f) }
  | LParen expr RParen
      { ParenExpr (($startpos, $endpos),$2) }

member_expr
  : primary_expr 
      { $1 }
  | Function LParen ids RParen LBrace src_elts RBrace
    { FuncExpr (($startpos, $endpos), $3, 
                BlockStmt (($startpos($5), $endpos($7)), $6)) }
/* Reduce/reduce conflict with function statements.  Who here knew that
   named function expressions existed?  
  | Function Id LParen ids RParen LBrace src_elts RBrace
    { let _,x = $2 in NamedFuncExpr ($1, x, $4, BlockStmt ($6, $7)) }
*/
  | member_expr Period Id 
      { let _,x = $3 in DotExpr (($startpos, $endpos),$1,x) } 
{'fraction_non_alphanumeric': 0.15151880424300868, 'fraction_numerical': 0.01518804243008679, 'mean_word_length': 3.1547320981472207, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3502232', 'n_tokens_mistral': 3177, 'n_tokens_neox': 3037, 'n_words': 903}