1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
|
/* $Id: script.h $ */
/** @file
* IPRT - RTScript, Script language support in IPRT.
*/
/*
* Copyright (C) 2024-2025 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef IPRT_INCLUDED_script_h
#define IPRT_INCLUDED_script_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/cdefs.h>
#include <iprt/types.h>
#include <iprt/strcache.h>
#include <iprt/scriptbase.h>
//#include <iprt/scriptast.h>
RT_C_DECLS_BEGIN
/** @defgroup grp_rt_script RTScript - IPRT scripting language support
* @ingroup grp_rt
*
* The scripting APIs provide a simple framework to implement simple scripting
* languages. They are meant to provide building blocks which can be put together
* in an easy way to add scripting capablities to the software using it.
*
* The API is oriented on the traditional compiler pipeline providing sub APIs for the following
* parts:
* - RTScriptLex*: For building a lexer to generate tokens from an input character stream.
* - RTScriptTs*: A simple type system providing a way to get type storage sizes and alignments.
* - RTScriptPs*: For maintaining the required state for the complete script and provide
* a way to check for correct typing.
* - RTScriptAst*: Providing helpers and definitions for the abstract syntax tree.
* - RTScriptParse*: For building parsers which generate ASTs.
* - RTScriptVm*: Providing a simple bytecode VM which takes a checked program state
* converting it into bytecode and executing it.
*
* Note: Only RTScriptLex is partially implemented right now!
* @{
*/
/** @defgroup grp_rt_script_lex Scripting lexer support
*
* This part provides support for lexing input and generating tokens which can be
* digested by a parser.
*
* @{
*/
/**
* The lexer token type.
*/
typedef enum RTSCRIPTLEXTOKTYPE
{
/** Invalid type. */
RTSCRIPTLEXTOKTYPE_INVALID = 0,
/** Identifier. */
RTSCRIPTLEXTOKTYPE_IDENTIFIER,
/** Numerical constant. */
RTSCRIPTLEXTOKTYPE_NUMBER,
/** String literal. */
RTSCRIPTLEXTOKTYPE_STRINGLIT,
/** An operator (unary or binary). */
RTSCRIPTLEXTOKTYPE_OPERATOR,
/** Some predefined keyword. */
RTSCRIPTLEXTOKTYPE_KEYWORD,
/** Some punctuator. */
RTSCRIPTLEXTOKTYPE_PUNCTUATOR,
/** A single line comment. */
RTSCRIPTLEXTOKTYPE_COMMENT_SINGLE_LINE,
/** A multi line comment. */
RTSCRIPTLEXTOKTYPE_COMMENT_MULTI_LINE,
/** Special error token, conveying an error message from the lexer. */
RTSCRIPTLEXTOKTYPE_ERROR,
/** End of stream token. */
RTSCRIPTLEXTOKTYPE_EOS
} RTSCRIPTLEXTOKTYPE;
/** Pointer to a lexer token type. */
typedef RTSCRIPTLEXTOKTYPE *PRTSCRIPTLEXTOKTYPE;
/**
* Lexer token number type.
*/
typedef enum RTSCRIPTLEXTOKNUMTYPE
{
/** Invalid token number type. */
RTSCRIPTLEXTOKNUMTYPE_INVALID = 0,
/** Natural number (all positive upwards including 0). */
RTSCRIPTLEXTOKNUMTYPE_NATURAL,
/** Integers (natural numbers and their additive inverse). */
RTSCRIPTLEXTOKNUMTYPE_INTEGER,
/** Real numbers. */
RTSCRIPTLEXTOKNUMTYPE_REAL
} RTSCRIPTLEXTOKNUMTYPE;
/**
* Lexer exact token match descriptor.
*/
typedef struct RTSCRIPTLEXTOKMATCH
{
/** Matching string. */
const char *pszMatch;
/** Size if of the matching string in characters excluding the zero terminator. */
size_t cchMatch;
/** Resulting token type. */
RTSCRIPTLEXTOKTYPE enmTokType;
/** Whether the token can be the beginning of an identifer
* and to check whether the identifer has a longer match. */
bool fMaybeIdentifier;
/** User defined value when the token matched. */
uint64_t u64Val;
} RTSCRIPTLEXTOKMATCH;
/** Pointer to a lexer exact token match descriptor. */
typedef RTSCRIPTLEXTOKMATCH *PRTSCRIPTLEXTOKMATCH;
/** Pointer to a const lexer exact token match descriptor. */
typedef const RTSCRIPTLEXTOKMATCH *PCRTSCRIPTLEXTOKMATCH;
/**
* Lexer token.
*/
typedef struct RTSCRIPTLEXTOKEN
{
/** Token type. */
RTSCRIPTLEXTOKTYPE enmType;
/** Position in the source buffer where the token started matching. */
RTSCRIPTPOS PosStart;
/** Position in the source buffer where the token ended matching. */
RTSCRIPTPOS PosEnd;
/** Data based on the token type. */
union
{
/** Identifier. */
struct
{
/** Pointer to the start of the identifier. */
const char *pszIde;
/** Number of characters for the identifier excluding the null terminator. */
size_t cchIde;
} Id;
/** Numerical constant. */
struct
{
/** Flag whether the number is negative. */
RTSCRIPTLEXTOKNUMTYPE enmType;
/** Optimal storage size for the value (1, 2, 4 or 8 bytes). */
uint32_t cBytes;
/** Type dependent data. */
union
{
/** For natural numbers. */
uint64_t u64;
/** For integer numbers. */
int64_t i64;
/** Real numbers. */
RTFLOAT64U r64;
} Type;
} Number;
/** String literal */
struct
{
/** Pointer to the start of the string constant. */
const char *pszString;
/** Number of characters of the string, including the null terminator. */
size_t cchString;
} StringLit;
/** Operator */
struct
{
/** Pointer to the operator descriptor. */
PCRTSCRIPTLEXTOKMATCH pOp;
} Operator;
/** Keyword. */
struct
{
/** Pointer to the keyword descriptor. */
PCRTSCRIPTLEXTOKMATCH pKeyword;
} Keyword;
/** Punctuator. */
struct
{
/** Pointer to the matched punctuator descriptor. */
PCRTSCRIPTLEXTOKMATCH pPunctuator;
} Punctuator;
/** Comment */
struct
{
/** Pointer to the start of the comment (including the symbols starting the comment). */
const char *pszComment;
/** Number of characters of the comment, including the null terminator. */
size_t cchComment;
} Comment;
/** Error. */
struct
{
/** Pointer to the internal error info structure, readonly. */
PCRTERRINFO pErr;
} Error;
} Type;
} RTSCRIPTLEXTOKEN;
/** Pointer to a script token. */
typedef RTSCRIPTLEXTOKEN *PRTSCRIPTLEXTOKEN;
/** Pointer to a const script token. */
typedef const RTSCRIPTLEXTOKEN *PCRTSCRIPTLEXTOKEN;
/** Opaque lexer handle. */
typedef struct RTSCRIPTLEXINT *RTSCRIPTLEX;
/** Pointer to a lexer handle. */
typedef RTSCRIPTLEX *PRTSCRIPTLEX;
/**
* Production rule callback.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param ch The character which caused the production rule to be called.
* @param pToken The token to fill in.
* @param pvUser Opaque user data.
*/
typedef DECLCALLBACKTYPE(int, FNRTSCRIPTLEXPROD,(RTSCRIPTLEX hScriptLex, char ch, PRTSCRIPTLEXTOKEN pToken, void *pvUser));
/** Pointer to a production rule callback. */
typedef FNRTSCRIPTLEXPROD *PFNRTSCRIPTLEXPROD;
/**
* Lexer rule.
*/
typedef struct RTSCRIPTLEXRULE
{
/** First character for starting the production rule. */
char chStart;
/** Last character for starting the production rule. */
char chEnd;
/** Flags for this rule. */
uint32_t fFlags;
/** The producer to call. */
PFNRTSCRIPTLEXPROD pfnProd;
/** Opaque user data passed to the production rule. */
void *pvUser;
} RTSCRIPTLEXRULE;
/** Pointer to a lexer rule. */
typedef RTSCRIPTLEXRULE *PRTSCRIPTLEXRULE;
/** Pointer to a const lexer rule. */
typedef const RTSCRIPTLEXRULE *PCRTSCRIPTLEXRULE;
/** Default rule flags. */
#define RTSCRIPT_LEX_RULE_DEFAULT 0
/** Consume the first character before calling the producer. */
#define RTSCRIPT_LEX_RULE_CONSUME RT_BIT(0)
/**
* Lexer config.
*/
typedef struct RTSCRIPTLEXCFG
{
/** Config name. */
const char *pszName;
/** Config description. */
const char *pszDesc;
/** Flags for the lexer. */
uint32_t fFlags;
/** Set of whitespace characters, excluding newline. NULL indicates default characters.
* " " | "\t". */
const char *pszWhitespace;
/** Set of newline characters, NULL indicates
* default characters including "\n" | "\r\n". */
const char **papszNewline;
/** Start for a multiline comment, NULL indicates no support for multi line comments. */
const char **papszCommentMultiStart;
/** End for a multiline comment, NULL indicates no support for multi line comments. */
const char **papszCommentMultiEnd;
/** Start of single line comment, NULL indicates no support for single line comments. */
const char **papszCommentSingleStart;
/** Exact token match descriptor table, optional. Must be terminated with a NULL entry. */
PCRTSCRIPTLEXTOKMATCH paTokMatches;
/** Pointer to the rule table, optional. */
PCRTSCRIPTLEXRULE paRules;
/** The default rule to call when no other rule applies, optional. */
PFNRTSCRIPTLEXPROD pfnProdDef;
/** Opaque user data for default production rule. */
void *pvProdDefUser;
} RTSCRIPTLEXCFG;
/** Pointer to a lexer config. */
typedef RTSCRIPTLEXCFG *PRTSCRIPTLEXCFG;
/** Pointer to a const lexer config. */
typedef const RTSCRIPTLEXCFG *PCRTSCRIPTLEXCFG;
/** Default lexer config flags. */
#define RTSCRIPT_LEX_CFG_F_DEFAULT 0
/** Case insensitive lexing, keywords and so on must be used lowercase to match
* as the lexer will convert everything to lowercase internally. */
#define RTSCRIPT_LEX_CFG_F_CASE_INSENSITIVE_LOWER RT_BIT(0)
/** Case insensitive lexing, keywords and so on must be used uppercase to match
* as the lexer will convert everything to uppercase internally. */
#define RTSCRIPT_LEX_CFG_F_CASE_INSENSITIVE_UPPER RT_BIT(1)
/** Comments are not skipped but passed back as tokens. */
#define RTSCRIPT_LEX_CFG_F_COMMENTS_AS_TOKENS RT_BIT(2)
/** Default character conversions (converting to lower case when the case insensitive flag is set). */
#define RTSCRIPT_LEX_CONV_F_DEFAULT 0
/** Don't apply any conversion but just return the character as read from the input. */
#define RTSCRIPT_LEX_CONV_F_NOTHING RT_BIT(0)
/**
* Lexer reader callback.
*
* @returns IPRT status code.
* @retval VINF_EOF if the end of the input was reached.
* @param hScriptLex The lexer handle.
* @param offBuf Offset from the start to read from.
* @param pchBuf Where to store the read data.
* @param cchBuf How much to read at maxmimum.
* @param pcchRead Where to store the amount of bytes read.
* @param pvUser Opaque user data passed when creating the lexer.
*/
typedef DECLCALLBACKTYPE(int, FNRTSCRIPTLEXRDR, (RTSCRIPTLEX hScriptLex, size_t offBuf, char *pchBuf, size_t cchBuf,
size_t *pcchRead, void *pvUser));
/** Pointer to a lexer reader callback. */
typedef FNRTSCRIPTLEXRDR *PFNRTSCRIPTLEXRDR;
/**
* Lexer destructor callback.
*
* @param hScriptLex The lexer handle.
* @param pvUser Opaque user data passed when creating the lexer.
*/
typedef DECLCALLBACKTYPE(void, FNRTSCRIPTLEXDTOR,(RTSCRIPTLEX hScriptLex, void *pvUser));
/** Pointer to a lexer destructor callback. */
typedef FNRTSCRIPTLEXDTOR *PFNRTSCRIPTLEXDTOR;
/**
* Creates a new lexer with the given reader and config.
*
* @returns IPRT status code.
* @param phScriptLex Where to store the handle to the lexer on success.
* @param pfnReader The read to use for reading chunks of the input.
* @param pfnDtor Destructor callback to call when the lexer is destroyed.
* @param pvUser Opaque user data passed to reader.
* @param cchBuf Buffer hint, if 0 a default is chosen.
* @param phStrCacheId Where to store the pointer to the string cache containing all
* scanned identifiers on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheStringLit Where to store the pointer to the string cache containing all
* scanned string literals on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheComments Where to store the pointer to the string cache containing all
* comments on success when RTSCRIPT_LEX_CFG_F_COMMENTS_AS_TOKENS
* is given, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param pCfg The lexer config to use for identifying the different tokens.
*/
RTDECL(int) RTScriptLexCreateFromReader(PRTSCRIPTLEX phScriptLex, PFNRTSCRIPTLEXRDR pfnReader,
PFNRTSCRIPTLEXDTOR pfnDtor, void *pvUser,
size_t cchBuf, PRTSTRCACHE phStrCacheId, PRTSTRCACHE phStrCacheStringLit,
PRTSTRCACHE phStrCacheComments, PCRTSCRIPTLEXCFG pCfg);
/**
* Creates a new lexer for the given input string and config.
*
* @returns IPRT status code.
* @param phScriptLex Where to store the handle to the lexer on success.
* @param pszSrc The input string to scan.
* @param phStrCacheId Where to store the pointer to the string cache containing all
* scanned identifiers on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheStringLit Where to store the pointer to the string cache containing all
* scanned string literals on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheComments Where to store the pointer to the string cache containing all
* comments on success when RTSCRIPT_LEX_CFG_F_COMMENTS_AS_TOKENS
* is given, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param pCfg The lexer config to use for identifying the different tokens.
*/
RTDECL(int) RTScriptLexCreateFromString(PRTSCRIPTLEX phScriptLex, const char *pszSrc, PRTSTRCACHE phStrCacheId,
PRTSTRCACHE phStrCacheStringLit, PRTSTRCACHE phStrCacheComments,
PCRTSCRIPTLEXCFG pCfg);
/**
* Creates a new lexer from the given filename and config.
*
* @returns IPRT status code.
* @param phScriptLex Where to store the handle to the lexer on success.
* @param pszFilename The filename of the input.
* @param phStrCacheId Where to store the pointer to the string cache containing all
* scanned identifiers on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheStringLit Where to store the pointer to the string cache containing all
* scanned string literals on success, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param phStrCacheComments Where to store the pointer to the string cache containing all
* comments on success when RTSCRIPT_LEX_CFG_F_COMMENTS_AS_TOKENS
* is given, optional.
* If not NULL the string cache must be freed by the caller when not used
* anymore.
* @param pCfg The lexer config to use for identifying the different tokens.
*/
RTDECL(int) RTScriptLexCreateFromFile(PRTSCRIPTLEX phScriptLex, const char *pszFilename, PRTSTRCACHE phStrCacheId,
PRTSTRCACHE phStrCacheStringLit, PRTSTRCACHE phStrCacheComments,
PCRTSCRIPTLEXCFG pCfg);
/**
* Destroys the given lexer handle.
*
* @param hScriptLex The lexer handle to destroy.
*/
RTDECL(void) RTScriptLexDestroy(RTSCRIPTLEX hScriptLex);
/**
* Queries the current identified token.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param ppToken Where to store the pointer to the token on success.
*/
RTDECL(int) RTScriptLexQueryToken(RTSCRIPTLEX hScriptLex, PCRTSCRIPTLEXTOKEN *ppToken);
/**
* Returns the current token type.
*
* @returns Current token type.
* @param hScriptLex The lexer handle.
*/
RTDECL(RTSCRIPTLEXTOKTYPE) RTScriptLexGetTokenType(RTSCRIPTLEX hScriptLex);
/**
* Returns the next token type.
*
* @returns Next token type.
* @param hScriptLex The lexer handle.
*/
RTDECL(RTSCRIPTLEXTOKTYPE) RTScriptLexPeekNextTokenType(RTSCRIPTLEX hScriptLex);
/**
* Consumes the current token and moves to the next one.
*
* @returns Pointer to the next token.
* @param hScriptLex The lexer handle.
*/
RTDECL(PCRTSCRIPTLEXTOKEN) RTScriptLexConsumeToken(RTSCRIPTLEX hScriptLex);
/**
* Consumes the current input characters and moves to the next one.
*
* @returns Returns the next character in the input stream.
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
*/
RTDECL(char) RTScriptLexConsumeCh(RTSCRIPTLEX hScriptLex);
/**
* Consumes the current input characters and moves to the next one - extended version.
*
* @returns Returns the next character in the input stream.
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
* @param fFlags Flags controlling some basic conversions of characters,
* combination of RTSCRIPT_LEX_CONV_F_*.
*/
RTDECL(char) RTScriptLexConsumeChEx(RTSCRIPTLEX hScriptLex, uint32_t fFlags);
/**
* Returns the character at the curren input position.
*
* @returns Character at the current position in the input
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
*/
RTDECL(char) RTScriptLexGetCh(RTSCRIPTLEX hScriptLex);
/**
* Returns the character at the curren input position - extended version.
*
* @returns Character at the current position in the input
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
* @param fFlags Flags controlling some basic conversions of characters,
* combination of RTSCRIPT_LEX_CONV_F_*.
*/
RTDECL(char) RTScriptLexGetChEx(RTSCRIPTLEX hScriptLex, uint32_t fFlags);
/**
* Returns the current character in the input without moving to the next one.
*
* @returns Returns the current character.
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
* @param idx Offset to peek at, 0 behaves like rtScriptLexGetCh().
*/
RTDECL(char) RTScriptLexPeekCh(RTSCRIPTLEX hScriptLex, unsigned idx);
/**
* Returns the current character in the input without moving to the next one - extended version.
*
* @returns Returns the current character.
* @retval 0 indicates end of stream.
* @param hScriptLex The lexer handle.
* @param idx Offset to peek at, 0 behaves like rtScriptLexGetCh().
* @param fFlags Flags controlling some basic conversions of characters,
* combination of RTSCRIPT_LEX_CONV_F_*.
*/
RTDECL(char) RTScriptLexPeekChEx(RTSCRIPTLEX hScriptLex, unsigned idx, uint32_t fFlags);
/**
* Skips everything declared as whitespace, including comments and newlines.
*
* @param hScriptLex The lexer handle.
*/
RTDECL(void) RTScriptLexSkipWhitespace(RTSCRIPTLEX hScriptLex);
/** @defgroup grp_rt_script_lex_builtin Builtin helpers to scan numbers, string literals, ...
* @{ */
/**
* Produces a numerical constant token from the number starting at the current position in the
* input stream on success or an appropriate error token.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param uBase The base to parse the number in.
* @param fAllowReal Flag whether to allow parsing real numbers using the following
* layout [+-][0-9]*[.][e][+-][0-9]*.
* @param pTok The token to fill in.
*/
RTDECL(int) RTScriptLexScanNumber(RTSCRIPTLEX hScriptLex, uint8_t uBase, bool fAllowReal,
PRTSCRIPTLEXTOKEN pTok);
/**
* Production rule to create an identifier token with the given set of allowed characters.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param ch The first character of the identifier.
* @param pTok The token to fill in.
* @param pvUser Opaque user data, must point to the allowed set of characters for identifiers as a
* zero terminated string. NULL will revert to a default set of characters including
* [_a-zA-Z0-9]*
*
* @note This version will allow a maximum identifier length of 512 characters (should be plenty).
* More characters will produce an error token. Must be used with the RTSCRIPT_LEX_RULE_CONSUME
* flag for the first character.
*/
RTDECL(int) RTScriptLexScanIdentifier(RTSCRIPTLEX hScriptLex, char ch, PRTSCRIPTLEXTOKEN pTok, void *pvUser);
/**
* Production rule to scan string literals conforming to the C standard.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param ch The character starting the rule, unused.
* @param pTok The token to fill in.
* @param pvUser Opaque user data, unused.
*
* @note The RTSCRIPT_LEX_RULE_CONSUME must be used (or omitted) such that the current character
* in the input denotes the start of the string literal. The resulting literal is added to the respective
* cache on success.
*/
RTDECL(int) RTScriptLexScanStringLiteralC(RTSCRIPTLEX hScriptLex, char ch, PRTSCRIPTLEXTOKEN pTok, void *pvUser);
/**
* Production rule to scan string literals for pascal like languages, without support for escape
* sequences and where a ' is denoted by ''.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param ch The character starting the rule, unused.
* @param pTok The token to fill in.
* @param pvUser Opaque user data, unused.
*
* @note The RTSCRIPT_LEX_RULE_CONSUME must be used (or omitted) such that the current character
* in the input denotes the start of the string literal. The resulting literal is added to the respective
* cache on success.
*/
RTDECL(int) RTScriptLexScanStringLiteralPascal(RTSCRIPTLEX hScriptLex, char ch, PRTSCRIPTLEXTOKEN pTok, void *pvUser);
/**
* Produces an error token with the given message, used for custom lexer rule implementations.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param pTok The token to fill.
* @param rc The status code to use in the message.
* @param pszMsg The format string for the error message.
* @param ... Arguments to the format string.
*/
RTDECL(int) RTScriptLexProduceTokError(RTSCRIPTLEX hScriptLex, PRTSCRIPTLEXTOKEN pTok, int rc, const char *pszMsg, ...);
/**
* Produces an identifier token with the given identifier, used for custom lexer rule implementations.
*
* @returns IPRT status code.
* @param hScriptLex The lexer handle.
* @param pTok The token to fill.
* @param pszIde The identifier to add.
* @param cchIde Number of characters in the identifier.
*/
RTDECL(int) RTScriptLexProduceTokIde(RTSCRIPTLEX hScriptLex, PRTSCRIPTLEXTOKEN pTok, const char *pszIde, size_t cchIde);
/** @} */
/** @} */
#if 0 /* Later, maybe */
/** @defgroup grp_rt_script_typesys Scripting language type system API.
*
* @{
*/
/**
* Type class.
*/
typedef enum RTSCRIPTTSTYPECLASS
{
/** Invalid. */
RTSCRIPTTSTYPECLASS_INVALID = 0,
/** A native type. */
RTSCRIPTTSTYPECLASS_NATIVE,
/** Array type. */
RTSCRIPTTSTYPECLASS_ARRAY,
/** Struct type. */
RTSCRIPTTSTYPECLASS_STRUCT,
/** Union type. */
RTSCRIPTTSTYPECLASS_UNION,
/** Function type. */
RTSCRIPTTSTYPECLASS_FUNCTION,
/** Pointer type. */
RTSCRIPTTSTYPECLASS_POINTER,
/** Alias for another type. */
RTSCRIPTTSTYPECLASS_ALIAS
} RTSCRIPTTSTYPECLASS;
/** Pointer to a type descriptor. */
typedef struct RTSCRIPTTSTYPDESC *PRTSCRIPTTSTYPDESC;
/** Pointer to a const type descriptor. */
typedef const RTSCRIPTTSTYPDESC *PCRTSCRIPTTSTYPDESC;
/**
* Type descriptor.
*/
typedef struct RTSCRIPTTSTYPDESC
{
/** Type class */
RTSCRIPTTSTYPECLASS enmClass;
/** Identifier for this type. */
const char *pszIdentifier;
/** Class dependent data. */
union
{
/** Native type. */
struct
{
/** The native type. */
RTSCRIPTNATIVETYPE enmTypeNative;
/** Alignment for the native type in bits - 0 for default alignment. */
uint32_t cBitsAlign;
} Native;
/** Array type. */
struct
{
/** Type identifier. */
const char *pszTypeIde;
/** Number of elements. */
uint32_t cElems;
} Array;
/** Struct/Union type. */
struct
{
/* Flag whether this is packed. */
bool fPacked;
/** Number of members. */
uint32_t cMembers;
/** Pointer to the array of member identifiers for each member. */
const char **papszMember;
/** Pointer to the array of type identifiers for each member. */
const char **papszMemberType;
} UnionStruct;
/** Function type. */
struct
{
/** Return type - NULL for no return type. */
const char *pszTypeRet;
/** Number of typed arguments. */
uint32_t cArgsTyped;
/** Pointer to the array of type identifiers for the arguments. */
const char **papszMember;
/** Flag whether variable arguments are used. */
bool fVarArgs;
} Function;
/** Pointer. */
struct
{
/** The type identifier the pointer references. */
const char *pszTypeIde;
} Pointer;
/** Pointer. */
struct
{
/** The type identifier the alias references. */
const char *pszTypeIde;
} Alias;
} Class;
} RTSCRIPTTSTYPDESC;
/** Opaque type system handle. */
typedef struct RTSCRIPTTSINT *RTSCRIPTTS;
/** Pointer to an opaque type system handle. */
typedef RTSCRIPTTS *PRTSCRIPTTS;
/** Opaque type system type. */
typedef struct RTSCRIPTTSTYPEINT *RTSCRIPTTSTYPE;
/** Pointer to an opaque type system type. */
typedef RTSCRIPTTSTYPE *PRTSCRIPTTSTYPE;
/**
* Create a new empty type system.
*
* @returns IPRT status code.
* @param phScriptTs Where to store the handle to the type system on success.
* @param hScriptTsParent Parent type system to get declarations from. NULL if no parent.
* @param enmPtrType Native pointer type (only unsigned integer types allowed).
* @param cPtrAlignBits The native alignment of a pointer storage location.
*/
RTDECL(int) RTScriptTsCreate(PRTSCRIPTTS phScriptTs, RTSCRIPTTS hScriptTsParent,
RTSCRIPTNATIVETYPE enmPtrType, uint32_t cPtrAlignBits);
/**
* Retains a reference to the given type system.
*
* @returns New reference count.
* @param hScriptTs Type system handle.
*/
RTDECL(uint32_t) RTScriptTsRetain(RTSCRIPTTS hScriptTs);
/**
* Releases a reference to the given type system.
*
* @returns New reference count, on 0 the type system is destroyed.
* @param hScriptTs Type system handle.
*/
RTDECL(uint32_t) RTScriptTsRelease(RTSCRIPTTS hScriptTs);
/**
* Dumps the content of the type system.
*
* @returns IPRT status code.
* @param hScriptTs Type system handle.
*/
RTDECL(int) RTScriptTsDump(RTSCRIPTTS hScriptTs);
/**
* Add several types to the type system from the given descriptor array.
*
* @returns IPRT status code.
* @param hScriptTs Type system handle.
* @param paTypeDescs Pointer to the array of type descriptors.
* @param cTypeDescs Number of entries in the array.
*/
RTDECL(int) RTScriptTsAdd(RTSCRIPTTS hScriptTs, PCRTSCRIPTTSTYPDESC paTypeDescs,
uint32_t cTypeDescs);
/**
* Removes the given types from the type system.
*
* @returns IPRT status code.
* @param hScriptTs Type system handle.
* @param papszTypes Array of type identifiers to remove. Array terminated
* with a NULL entry.
*/
RTDECL(int) RTScriptTsRemove(RTSCRIPTTS hScriptTs, const char **papszTypes);
/**
* Queries the given type returning the type handle on success.
*
* @returns IPRT status code.
* @retval VERR_NOT_FOUND if the type could not be found.
* @param hScriptTs Type system handle.
* @param pszType The type identifier to look for.
* @param phType Where to store the handle to the type on success.
*/
RTDECL(int) RTScriptTsQueryType(RTSCRIPTTS hScriptTs, const char *pszType,
PRTSCRIPTTSTYPE phType);
/**
* Retains the given type reference.
*
* @returns New reference count.
* @param hScriptTsType Type system type handle.
*/
RTDECL(uint32_t) RTScriptTsTypeRetain(RTSCRIPTTSTYPE hScriptTsType);
/**
* Releases the given type reference.
*
* @returns New reference count.
* @param hScriptTsType Type system type handle.
*/
RTDECL(uint32_t) RTScriptTsTypeRelease(RTSCRIPTTSTYPE hScriptTsType);
/**
* Returns the class of the given type handle.
*
* @returns Type class for the given type handle.
* @param hScriptTsType Type system type handle.
*/
RTDECL(RTSCRIPTTSTYPECLASS) RTScriptTsTypeGetClass(RTSCRIPTTSTYPE hScriptTsType);
/**
* Returns the identifier of the given type handle.
*
* @returns Pointer to the identifier of the given type handle.
* @param hScriptTsType Type system type handle.
*/
RTDECL(const char *) RTScriptTsTypeGetIdentifier(RTSCRIPTTSTYPE hScriptTsType);
/**
* Returns the storage size of the given type in bits.
*
* @returns Size of the type in bits described by the given handle.
* @param hScriptTsType Type system type handle.
*/
RTDECL(size_t) RTScriptTsTypeGetBitCount(RTSCRIPTTSTYPE hScriptTsType);
/**
* Returns the necessary alignment of the given type in bits.
*
* @returns Alignmebt of the type in bits described by the given handle.
* @param hScriptTsType Type system type handle.
*/
RTDECL(size_t) RTScriptTsTypeGetAlignmentBitCount(RTSCRIPTTSTYPE hScriptTsType);
/**
* Return the native type for the given native type.
*
* @returns Native type enum.
* @param hScriptTsType Type system type handle.
*/
RTDECL(RTSCRIPTNATIVETYPE) RTScriptTsTypeNativeGetType(RTSCRIPTTSTYPE hScriptTsType);
/**
* Return the number of elements for the given array type.
*
* @returns Number of elements.
* @param hScriptTsType Type system type handle.
*/
RTDECL(uint32_t) RTScriptTsTypeArrayGetElemCount(RTSCRIPTTSTYPE hScriptTsType);
/**
* Return the type handle of element type for the given array type.
*
* @returns Number of elements.
* @param hScriptTsType Type system type handle.
*/
RTDECL(RTSCRIPTTSTYPE) RTScriptTsTypeArrayGetElemType(RTSCRIPTTSTYPE hScriptTsType);
/**
* Returns whether the given union/struct type is packed.
*
* @returns Number of elements.
* @param hScriptTsType Type system type handle.
*/
RTDECL(bool) RTScriptTsTypeStructUnionGetPacked(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(uint32_t) RTScriptTsTypeStructUnionGetMemberCount(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(int) RTScriptTsTypeStructUnionQueryMember(RTSCRIPTTSTYPE hScriptTsType, uint32_t idxMember, uint32_t *poffMember,
uint32_t *pcMemberBits, PRTSCRIPTTSTYPE phScriptTsTypeMember);
RTDECL(RTSCRIPTTSTYPE) RTScriptTsTypeFunctionGetRetType(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(uint32_t) RTScriptTsTypeFunctionGetTypedArgCount(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(bool) RTScriptTsTypeFunctionUsesVarArgs(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(RTSCRIPTTSTYPE) RTScriptTsTypeFunctionGetArgType(RTSCRIPTTSTYPE hScriptTsType, uint32_t idxArg);
RTDECL(RTSCRIPTTSTYPE) RTScriptTsTypePointerGetRefType(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(RTSCRIPTTSTYPE) RTScriptTsTypeAliasGetAliasedType(RTSCRIPTTSTYPE hScriptTsType);
RTDECL(bool) RTScriptTsTypeEquals(RTSCRIPTTSTYPE hScriptTsType1, RTSCRIPTTSTYPE hScriptTsType2);
RTDECL(bool) RTScriptTsTypeEqualsByOneName(RTSCRIPTTS hScriptTs, const char *pszType1, RTSCRIPTTSTYPE hScriptTsType2);
RTDECL(bool) RTScriptTsTypeEqualsByTwoNames(RTSCRIPTTS hScriptTs, const char *pszType1, const char *pszType2);
/** @} */
/** @defgroup grp_rt_script_ps Scripting program state API
*
* @{
*/
/** Opaque program state handle. */
typedef struct RTSCRIPTPSINT *RTSCRIPTPS;
/** Pointer to a program state handle. */
typedef RTSCRIPTPS *PRTSCRIPTPS;
RTDECL(int) RTScriptPsCreate(PRTSCRIPTPS phScriptPs, const char *pszId);
RTDECL(uint32_t) RTScriptPsRetain(RTSCRIPTPS hScriptPs);
RTDECL(uint32_t) RTScriptPsRelease(RTSCRIPTPS hScriptPs);
RTDECL(int) RTScriptPsDump(RTSCRIPTPS hScriptPs);
RTDECL(int) RTScriptPsBuildFromAst(RTSCRIPTPS hScriptPs, PRTSCRIPTASTCOMPILEUNIT pAstCompileUnit);
RTDECL(int) RTScriptPsCheckConsistency(RTSCRIPTPS hScriptPs);
/** @} */
/** @defgroup grp_rt_script_parse Scripting parsing API
*
* @{
*/
/**
* Creates a program state from the given pascal input source.
*
* @returns IPRT status code.
* @param phScriptPs Where to store the handle to the program state on success.
* @param pszId The program state ID.
* @param pszSrc The input to parse.
* @param pErrInfo Where to store error information, optional.
*/
RTDECL(int) RTScriptParsePascalFromString(PRTSCRIPTPS phScriptPs, const char *pszId, const char *pszSrc,
PRTERRINFO pErrInfo);
/** @} */
/** @defgroup grp_rt_script_vm Scripting bytecode VM API.
*
* @{
*/
/**
* Data value (for return values and arguments).
*/
typedef struct RTSCRIPTVMVAL
{
/** The value type. */
RTSCRIPTNATIVETYPE enmType;
/** Value, dependent on type. */
union
{
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
RTFLOAT64U r64;
} u;
} RTSCRIPTVMVAL;
/** Pointer to a VM value. */
typedef RTSCRIPTVMVAL *PRTSCRIPTVMVAL;
/** Pointer to a const value. */
typedef const RTSCRIPTVMVAL *PCRTSCRIPTVMVAL;
/** Opaque VM state handle. */
typedef struct RTSCRIPTVMINT *RTSCRIPTVM;
/** Pointer to a VM state handle. */
typedef RTSCRIPTVM *PRTSCRIPTVM;
/** Opaque VM execution context handle. */
typedef struct RTSCRIPTVMCTXINT *RTSCRIPTVMCTX;
/** Pointer to an opaque VM execution context handle. */
typedef RTSCRIPTVMCTX *PRTSCRIPTVMCTX;
/**
* Creates a new script VM.
*
* @returns IPRT status code.
* @param phScriptVm Where to store the VM handle on success.
*/
RTDECL(int) RTScriptVmCreate(PRTSCRIPTVM phScriptVm);
/**
* Retains the VM reference.
*
* @returns New reference count.
* @param hScriptVm The VM handle to retain.
*/
RTDECL(uint32_t) RTScriptVmRetain(RTSCRIPTVM hScriptVm);
/**
* Releases the VM reference.
*
* @returns New reference count, on 0 the VM is destroyed.
* @param hScriptVm The VM handle to release.
*/
RTDECL(uint32_t) RTScriptVmRelease(RTSCRIPTVM hScriptVm);
/**
* Adds the given program state to the VM making it available for execution.
*
* @returns IPRT status code.
* @param hScriptVm The VM handle.
* @param hScriptPs The program state to add.
* @param pErrInfo Where to store error information on failure.
*/
RTDECL(int) RTScriptVmAddPs(RTSCRIPTVM hScriptVm, RTSCRIPTPS hScriptPs, PRTERRINFO pErrInfo);
/**
* Creates a new execution context for running code in the VM.
*
* @returns IPRT status code.
* @param hScriptVm The VM handle.
* @param phScriptVmCtx Where to store the execution context handle on success.
* @param cbStack Size of the stack in bytes, 0 if unlimited.
*/
RTDECL(int) RTScriptVmExecCtxCreate(RTSCRIPTVM hScriptVm, PRTSCRIPTVMCTX phScriptVmCtx, size_t cbStack);
/**
* Retains the VM execution context reference.
*
* @returns New reference count.
* @param hScriptVmCtx The VM execution context handle to retain.
*/
RTDECL(uint32_t) RTScriptVmExecCtxRetain(RTSCRIPTVMCTX hScriptVmCtx);
/**
* Releases a VM execution context reference.
*
* @returns New reference count, on 0 the execution context is destroyed.
* @param hScriptVmCtx The VM execution context handle to release.
*/
RTDECL(uint32_t) RTScriptVmExecCtxRelease(RTSCRIPTVMCTX hScriptVmCtx);
/**
* Sets the initial state for execution.
*
* @returns IPRT status code.
* @param hScriptVmCtx The VM execution context handle.
* @param pszFn The method to execute, NULL for the main program if existing.
* @param paArgs Arguments to supply to the executed method.
* @param cArgs Number of arguments supplied.
*/
RTDECL(int) RTScriptVmExecCtxInit(RTSCRIPTVMCTX hScriptVmCtx, const char *pszFn,
PCRTSCRIPTVMVAL paArgs, uint32_t cArgs);
/**
* Continues executing the current state.
*
* @returns IPRT status code.
* @param hScriptVmCtx The VM execution context handle.
* @param msTimeout Maximum amount of time to execute, RT_INDEFINITE_WAIT
* for an unrestricted amount of time.
* @param pRetVal Where to store the return value on success, optional.
*/
RTDECL(int) RTScriptVmExecCtxRun(RTSCRIPTVMCTX hScriptVmCtx, RTMSINTERVAL msTimeout,
PRTSCRIPTVMVAL pRetVal);
/**
* Interrupts the current execution.
*
* @returns IPRT status code.
* @param hScriptVmCtx The VM execution context handle.
*/
RTDECL(int) RTScriptVmExecCtxInterrupt(RTSCRIPTVMCTX hScriptVmCtx);
/** @} */
#endif
/** @} */
RT_C_DECLS_END
#endif /* !IPRT_INCLUDED_script_h */
|