123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239(****************************************************************
* ASL lexer support
*
* Copyright Arm Limited (c) 2017-2019
* SPDX-Licence-Identifier: BSD-3-Clause
****************************************************************)(** ASL lexer support *)openLexingopenAsl_parserletstring_of_token(t:Asl_parser.token):string=(matchtwith|AMPERSAND->"amp"|AMPERSAND_AMPERSAND->"ampamp"|AND->"and"|ARRAY->"array"|ASSERT->"assert"|BANG->"bang"|BAR_BAR->"barbar"|BITS->"bits"|BITSLIT(x)->"bin:"^x|UNDERSCORE_UNDERSCORE_ARRAY->"__array"|UNDERSCORE_UNDERSCORE_BUILTIN->"__builtin"|UNDERSCORE_UNDERSCORE_CONDITIONAL->"__conditional"|UNDERSCORE_UNDERSCORE_CONFIG->"__config"|UNDERSCORE_UNDERSCORE_DECODE->"__decode"|UNDERSCORE_UNDERSCORE_ENCODING->"__encoding"|UNDERSCORE_UNDERSCORE_EXCEPTIONTAKEN->"__ExceptionTaken"|UNDERSCORE_UNDERSCORE_EXECUTE->"__execute"|UNDERSCORE_UNDERSCORE_EVENT->"__event"|UNDERSCORE_UNDERSCORE_FIELD->"__field"|UNDERSCORE_UNDERSCORE_FUNCTION->"__function"|UNDERSCORE_UNDERSCORE_GUARD->"__guard"|UNDERSCORE_UNDERSCORE_INSTRUCTION->"__instruction"|UNDERSCORE_UNDERSCORE_INSTRUCTION_UNDERSCORE_SET->"__instruction_set"|UNDERSCORE_UNDERSCORE_MAP->"__map"|UNDERSCORE_UNDERSCORE_NOP->"__NOP"|UNDERSCORE_UNDERSCORE_NEWEVENT->"__newevent"|UNDERSCORE_UNDERSCORE_NEWMAP->"__newmap"|UNDERSCORE_UNDERSCORE_OPCODE->"__opcode"|UNDERSCORE_UNDERSCORE_OPERATOR_ONE->"__operator1"|UNDERSCORE_UNDERSCORE_OPERATOR_TWO->"__operator2"|UNDERSCORE_UNDERSCORE_POSTDECODE->"__postdecode"|UNDERSCORE_UNDERSCORE_READWRITE->"__readwrite"|UNDERSCORE_UNDERSCORE_REGISTER->"__register"|UNDERSCORE_UNDERSCORE_UNALLOCATED->"__UNALLOCATED"|UNDERSCORE_UNDERSCORE_UNPREDICTABLE_UNDERSCORE_UNLESS->"__unpredictable_unless"|UNDERSCORE_UNDERSCORE_UNPREDICTABLE->"__UNPREDICTABLE"|UNDERSCORE_UNDERSCORE_WRITE->"__write"|CARET->"caret"|CASE->"case"|CATCH->"catch"|COLON->"colon"|COMMA->"comma"|CONSTANT->"constant"|CONSTRAINED_UNDERSCORE_UNPREDICTABLE->"constrained_unpredictable"|DEDENT->"dedent"|DIV->"div"|DO->"do"|DOT->"dot"|DOT_DOT->"dotdot"|DOWNTO->"downto"|ELSE->"else"|ELSIF->"elsif"|ENUMERATION->"enum"|EOF->"eof"|EOL1->"eol"|EOL2()->"eol"|EOR->"eor"|EQ->"eq"|EQ_EQ->"eqeq"|EQ_GT->"eqgt"|REALLIT(x)->"real:"^x|FOR->"for"|GT->"gt"|GT_EQ->"gteq"|GT_GT->"gtgt"|HEXLIT(x)->"hex:"^x|ID(x)->"ident:"^x|IF->"if"|IMPLEMENTATION_UNDERSCORE_DEFINED->"impdef"|IN->"in"|IFF->"iff"|IMPLIES->"implies"|INDENT->"indent"|INTLIT(x)->"int:"^x|IS->"is"|LBRACE->"lbrace"|LBRACE_LBRACE->"{{"|LBRACK->"lbrack"|LPAREN->"lparen"|LT->"lt"|LT_EQ->"lteq"|LT_LT->"ltlt"|MASKLIT(x)->"mask:"^x|MINUS->"minus"|MOD->"mod"|BANG_EQ->"neq"|NOT->"not"|OF->"of"|OR->"or"|OTHERWISE->"otherwise"|PLUS->"plus"|PLUS_PLUS->"plusplus"|PLUS_COLON->"pluscolon"|QUALIFIER(x)->"qualifier:"^x|QUOT->"quot"|RBRACE->"rbrace"|RBRACE_RBRACE->"}}"|RBRACK->"rbrack"|RECORD->"record"|REM->"rem"|REPEAT->"repeat"|RETURN->"return"|RPAREN->"rparen"|SEE->"see"|SEMICOLON->"semi"|SLASH->"slash"|STAR->"star"|STRINGLIT(x)->"string:"^x|THEN->"then"|THROW->"throw"|TYPEID(x)->"tident:"^x|TO->"to"|TRY->"try"|TYPE->"type"|TYPEOF->"typeof"|UNDEFINED->"undefined"|UNKNOWN->"unknown"|UNPREDICTABLE->"unpredictable"|UNTIL->"until"|WHEN->"when"|WHILE->"while")letprint_positionoutxlexbuf=letpos=lexbuf.lex_curr_pinPrintf.fprintfoutx"%s:%d:%d"pos.pos_fnamepos.pos_lnum(pos.pos_cnum-pos.pos_bol+1)letstarters:Asl_parser.tokenlist=[LPAREN;LBRACK;LBRACE;IF;ELSIF;WHILE]letenders:Asl_parser.tokenlist=[RPAREN;RBRACK;RBRACE;THEN;DO]typeoffside_state={mutablestack:intlist;(* indentation history *)mutableparens:int;(* number of outstanding openers *)mutablenewline:bool;(* processing newline *)mutablenext:Asl_parser.token;(* next token *)}letoffside_token(read:Lexing.lexbuf->Asl_parser.token):(Lexing.lexbuf->Asl_parser.token)=letstate={stack=[0];parens=0;newline=false;next=EOL1}inletpushStack(col:int):Asl_parser.token=beginstate.stack<-col::state.stack;INDENTendinletgetToken(buf:Lexing.lexbuf):Asl_parser.token=beginletuseToken_:Asl_parser.token=beginlettok:Asl_parser.token=state.nextinifList.memtokstartersthenbeginstate.parens<-state.parens+1endelseif(state.parens>0)&&(List.memtokenders)thenbeginstate.parens<-state.parens-1end;(trystate.next<-readbufwithLexer.Eof->state.next<-EOF);tokendinifstate.parens>0thenbegin(* In parentheses: ignore EOL tokens *)whilestate.next=EOL1doignore(useToken())done;useToken()endelseifstate.next=EOFthenbegin(* End of file: emit outstanding DEDENT tokens *)beginmatchstate.stackwith|[]|[_]->EOF|(d::ds)->state.stack<-ds;DEDENTendendelseifstate.next=EOL1thenbeginwhilestate.next=EOL1dostate.newline<-true;ignore(useToken())done;EOL1endelsebeginifstate.newlinethenbeginletprev_col=List.hdstate.stackinletpos=lexeme_start_pbufinletnew_column=pos.pos_cnum-pos.pos_bolinifnew_column>prev_colthenbeginstate.newline<-false;pushStacknew_columnendelseifnew_column=prev_colthenbeginstate.newline<-false;useToken()endelsebeginstate.stack<-List.tlstate.stack;lettarget_column=List.hdstate.stackin(* state.newline <- false; *)state.newline<-new_column<>target_column;(* This gives spurious warnings when indentation is
* decremented in two steps.
*
* if new_column < target_column then begin
* Printf.printf "Warning: incorrect indentation %d: %d %d\n"
* buf.lex_curr_p.pos_lnum
* new_column target_column
* end;
*)DEDENTendendelsebeginuseToken()endendendingetToken(****************************************************************
* End
****************************************************************)