Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219(**************************************************************************)(* Copyright (C) 2017-2023 Yann Régis-Gianas, Nicolas Jeannerod, *)(* Ralf Treinen. *)(* *)(* This is free software: you can redistribute it and/or modify it *)(* under the terms of the GNU General Public License, version 3. *)(* *)(* Additional terms apply, due to the reproduction of portions of *)(* the POSIX standard. Please refer to the file COPYING for details. *)(**************************************************************************)
(* Based on the original Morbig CST, this is a slightly slimpified AST focusing on being executable rather than capturing notions like position, linebreaks etc. *)open Import
type complete_commands = complete_command list[@@deriving yojson, traverse_map, traverse_fold]
and complete_command = clist * separator optionand and_or = And | Or | Noand_orand 'v separator_list = ('v, separator) Nslist.tand 'v and_or_list = ('v, and_or) Nslist.tand clist = pipeline and_or_list separator_listand pipeline = Pipeline of pipe_sequence | Pipeline_Bang of pipe_sequenceand pipe_sequence = command list
and command = | SimpleCommand of simple_command | CompoundCommand of compound_command * redirects | FunctionDefinition of function_definition
and compound_command = | BraceGroup of brace_group | Subshell of subshell | ForClause of for_clause | CaseClause of case_clause | IfClause of if_clause | WhileClause of while_clause | UntilClause of until_clause
and subshell = compound_listand compound_list = clist * separator
and for_clause = | For_Name_DoGroup of name * do_group | For_Name_In_WordList_DoGroup of name * wordlist * do_group
and wordlist = word Nlist.tand case_clause = Cases of word * case_list | Case of wordand case_list = case_item Nlist.tand case_item = Case_pattern of pattern * compound_list optionand pattern = word Nlist.t
and if_clause = | If_then_else of compound_list * compound_list * else_part | If_then of compound_list * compound_list
and else_part = | Elif_then of compound_list * compound_list | Elif_then_else of compound_list * compound_list * else_part | Else of compound_list
and while_clause = While of compound_list * do_groupand until_clause = Until of compound_list * do_groupand function_definition = string * function_bodyand function_body = compound_command * redirectsand brace_group = compound_listand do_group = compound_list
and simple_command = | Prefixed of cmd_prefix * cmd_word option * cmd_suffix option | Named of cmd_name * cmd_suffix option
and cmd_name = wordand cmd_word = word
and cmd_prefix_item = | Prefix_redirect of io_redirect | Prefix_assignment of assignment_word
and cmd_prefix = cmd_prefix_item listand cmd_suffix_item = Suffix_redirect of io_redirect | Suffix_word of wordand cmd_suffix = cmd_suffix_item listand redirects = io_redirect list
and io_redirect = | IoRedirect_IoFile of io_number * io_file | IoRedirect_IoHere of io_number * io_here
and io_op = | Io_op_less | Io_op_lessand | Io_op_great | Io_op_greatand | Io_op_dgreat | Io_op_lessgreat | Io_op_andgreat | Io_op_clobber
and io_file = io_op * filenameand filename = wordand io_here = IoHere of here_end * word | IoHere_Dash of here_end * wordand here_end = wordand separator = Ampersand | Semicolonand word = word_cstand word_cst = word_component list
and word_component = | WordSubshell of complete_commands | WordName of string | WordAssignmentWord of assignment_word | WordDoubleQuoted of word | WordSingleQuoted of word | WordTildePrefix of string | WordLiteral of string | WordVariable of variable | WordGlobAll (* asterisk *) | WordGlobAny (* question mark *) | WordArithmeticExpression of arith_expr | WordReBracketExpression of bracket_expression (* Empty CST. Useful to represent the absence of relevant CSTs. *) | WordEmpty
(* We handle _very_ simple arithmetic expressions. Really nothing crazy yet, hopefully enough to handle most [while x < 10 do x = x + 1 done] loops! *)and arith_operator = Add | Sub | Mul | Div | Mod | Lt | Gt | Eq
and arith_expr = | Int of int | Var of string | Binop of arith_operator * arith_expr * arith_expr | Neg of arith_expr | Assign of arith_operator * string * arith_expr | Ternary of (arith_expr * arith_expr * arith_expr)
and fragment = { txt : string; escaping : bool; was_quoted : bool; splittable : bool; globbable : bool; tilde_expansion : bool; join : [ `No | `With_previous | `With_next | `Yes ]; (* Used for "args: [$@]" *)}(** Post expansion representation of strings ready for possible field splitting and globbing. *)
and fragments = fragment list
and bracket_expression = | BracketExpression_LBRACKET_MatchingList_RBRACKET of matching_list | BracketExpression_LBRACKET_NonMatchingList_RBRACKET of nonmatching_list
and matching_list = MatchingList_BracketList of bracket_listand nonmatching_list = NonMatchingList_BracketList of bracket_list
and bracket_list = | BracketList_FollowList of follow_list | BracketList_FollowList_MINUS of follow_list
and follow_list = | FollowList_ExpressionTerm of expression_term | FollowList_FollowList_ExpressionTerm of follow_list * expression_term
and expression_term = | ExpressionTerm_SingleExpression of single_expression | ExpressionTerm_RangeExpression of range_expression
and single_expression = | SingleExpression_EndRange of end_range | SingleExpression_CharacterClass of character_class | SingleExpression_EquivalenceClass of equivalence_class
and range_expression = | RangeExpression_StartRange_EndRange of start_range * end_range | RangeExpression_StartRange_MINUS of start_range
and start_range = StartRange_EndRange_MINUS of end_range
and end_range = | EndRange_COLLELEMSINGLE of char | EndRangeCollatingSymbol of collating_symbol
and collating_symbol = | CollatingSymbol_OpenDot_COLLELEMSINGLE_DotClose of char | CollatingSymbol_OpenDot_COLLELEMMULTI_DotClose of string | CollatingSymbol_OpenDot_METACHAR_DotClose of char
and equivalence_class = | EquivalenceClass_OpenEqual_COLLELEMSINGLE_EqualClose of char | EquivalenceClass_OpenEqual_COLLELEMMULTI_EqualClose of string
and character_class = | CharacterClass_OpenColon_CLASSNAME_ColonClose of class_name
and class_name = | ClassName of string (* Keyword in the current LC_CTYPE category. *)
and character_range = Range of char listand variable = VariableAtom of string * variable_attribute
and variable_attribute = | NoAttribute | ParameterLength | UseDefaultValues of string * word | AssignDefaultValues of string * word | IndicateErrorifNullorUnset of string * word | UseAlternativeValue of string * word | RemoveSmallestSuffixPattern of word | RemoveLargestSuffixPattern of word | RemoveSmallestPrefixPattern of word | RemoveLargestPrefixPattern of word
and name = Name of stringand assignment_word = name * wordand io_number = int