Class Naftah
Naftah is an interpreted JVM language. This class handles command line argument parsing and execution of different commands such as running scripts, initializing classpath scanning, and starting an interactive shell (REPL).
Supports debug mode, encoding options, and classpath scanning configuration.
The supported file extensions for Naftah scripts are:
".naftah", ".nfth", ".na", ".nsh".
- Author:
- Chakib Daii
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classThe base command class for the Naftah CLI, implemented with picocli.static classProvides version information for the Naftah command line. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringProperty to enable Arabic-Indic digit formatting in Naftah.static final StringConfiguration key representing the classes of builtin function set.static final StringProperty to specify which builtin function set to use in Naftah.static final StringConfiguration key representing the packages of builtin function set.static final StringProperty to specify which package of builtin function set to use in Naftah.static final StringCache results of classpath and JDK scanning.static final StringDefault filename for the Naftah configuration file.static final StringProperty to specify the path to a Naftah configuration file.private booleanstatic final StringProperty to enable debug mode.static final StringProperty key for file encoding.static final StringProperty to force scanning the Java classpath.static final StringProperty to include jvm functions and instantiatable classes in repl completions, disabled by default.static final StringProperty set when inside the init command.static final StringProperty set when inside the man command.static final StringProperty set when inside the REPL (shell).static final StringProperty set when inside the run command.static final StringProperty to enable caching of string interpolation text processing in Naftah.private booleanstatic final StringSystem property enabling or disabling deep reflection for Java object mapping.static final StringSystem property specifying the maximum depth allowed during deep reflection.static final StringSystem property controlling whether null fields are skipped during reflection.static final StringSystem property key for the Java version.static final StringSystem property key for the Java Virtual Machine vendor.static final StringControls whether class and function definitions should be loaded on startup.private static LoggerLogger instance for logging Naftah program.static final StringProperty to enable caching of multiline text processing in Naftah.static final StringProperty to enable number formatter in Naftah.static final StringProperty to enable scanning the Java classpath for Naftah types.static final StringProperty to enable scanning the Jdk classes for Naftah types.private Stringstatic final String[]The recognized standard file extensions for Naftah scripts.static final StringConstant representing a single underscore character ("_").static final StringProperty to enable Vector API optimizations.static final StringProperty to enable chunks of long words in text processing in Naftah. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidInitializes Naftah configuration by loading default and optional external property files.private static voidinitLogger(boolean debug) Initializes the Java Util Logging configuration.static voidMain method of the Naftah interpreter.static voidPrints the error message of the given throwable with padding applied.private static voidPrints the string representation of the given object with padding applied.static voidprocessArgs(String[] args) Processes the command line arguments and dispatches the appropriate command.private static voidprocessConfig(Properties properties) Processes the given configuration properties and updates corresponding system properties for built-in function classes and packages.private booleanrun(Naftah.NaftahCommand naftahCommand) Runs the given NaftahCommand.private static voidBlocks until the user presses Ctrl+C, using the REPL terminal and line reader.
-
Field Details
-
JAVA_VERSION_PROPERTY
System property key for the Java version.- See Also:
-
JAVA_VM_VENDOR_PROPERTY
System property key for the Java Virtual Machine vendor.- See Also:
-
SCAN_CLASSPATH_PROPERTY
Property to enable scanning the Java classpath for Naftah types.- See Also:
-
FORCE_CLASSPATH_PROPERTY
Property to force scanning the Java classpath.- See Also:
-
INCLUDE_ALL_IN_COMPLETIONS_PROPERTY
Property to include jvm functions and instantiatable classes in repl completions, disabled by default.- See Also:
-
SCAN_JDK_PROPERTY
Property to enable scanning the Jdk classes for Naftah types.- See Also:
-
CACHE_SCANNING_RESULTS_PROPERTY
Cache results of classpath and JDK scanning.- See Also:
-
DEBUG_PROPERTY
Property to enable debug mode.- See Also:
-
INSIDE_REPL_PROPERTY
Property set when inside the REPL (shell).- See Also:
-
INSIDE_INIT_PROPERTY
Property set when inside the init command.- See Also:
-
INSIDE_MAN_PROPERTY
Property set when inside the man command.- See Also:
-
LOAD_CLASSES_AND_FUNCTIONS_PROPERTY
Controls whether class and function definitions should be loaded on startup.- See Also:
-
INSIDE_RUN_PROPERTY
Property set when inside the run command.- See Also:
-
FILE_ENCODING_PROPERTY
Property key for file encoding.- See Also:
-
VECTOR_API_PROPERTY
Property to enable Vector API optimizations.- See Also:
-
NUMBER_FORMATTER_PROPERTY
Property to enable number formatter in Naftah.- See Also:
-
ARABIC_INDIC_PROPERTY
Property to enable Arabic-Indic digit formatting in Naftah.- See Also:
-
MULTILINE_CACHE_PROPERTY
Property to enable caching of multiline text processing in Naftah.- See Also:
-
WORD_CHUNK_PROPERTY
Property to enable chunks of long words in text processing in Naftah.- See Also:
-
INTERPOLATION_CACHE_PROPERTY
Property to enable caching of string interpolation text processing in Naftah.- See Also:
-
CONFIG_FILE_PROPERTY
Property to specify the path to a Naftah configuration file.- See Also:
-
BUILTIN_CLASSES_PROPERTY
Property to specify which builtin function set to use in Naftah.- See Also:
-
BUILTIN_PACKAGES_PROPERTY
Property to specify which package of builtin function set to use in Naftah.- See Also:
-
JAVA_OBJECT_REFLECT_ACTIVE_PROPERTY
System property enabling or disabling deep reflection for Java object mapping.- See Also:
-
JAVA_OBJECT_REFLECT_MAX_DEPTH_PROPERTY
System property specifying the maximum depth allowed during deep reflection.- See Also:
-
JAVA_OBJECT_REFLECT_SKIP_NULLS_PROPERTY
System property controlling whether null fields are skipped during reflection.- See Also:
-
BUILTIN_CLASSES
Configuration key representing the classes of builtin function set.- See Also:
-
BUILTIN_PACKAGES
Configuration key representing the packages of builtin function set.- See Also:
-
CONFIG_FILE
Default filename for the Naftah configuration file.- See Also:
-
STANDARD_EXTENSIONS
The recognized standard file extensions for Naftah scripts. -
UNDERSCORE
Constant representing a single underscore character ("_").- See Also:
-
LOGGER
Logger instance for logging Naftah program. -
args
-
isScriptFile
private boolean isScriptFile -
script
-
debug
private boolean debug
-
-
Constructor Details
-
Naftah
private Naftah()Private constructor to prevent instantiation.
-
-
Method Details
-
main
Main method of the Naftah interpreter. Parses command line arguments and dispatches commands.- Parameters:
args- the command line arguments
-
initLogger
private static void initLogger(boolean debug) Initializes the Java Util Logging configuration. If debug is true, logging levels are set to verbose.- Parameters:
debug- true to enable debug logging, false otherwise
-
processConfig
Processes the given configuration properties and updates corresponding system properties for built-in function classes and packages.If the provided
propertiesinclude entries forBUILTIN_CLASSESorBUILTIN_PACKAGES, their values are appended to the existing system propertiesBUILTIN_CLASSES_PROPERTYandBUILTIN_PACKAGES_PROPERTY, respectively. This behavior ensures that built-in configurations are cumulative—existing values are preserved and new ones are appended.- Parameters:
properties- the configuration properties to process; may benull
-
initConfig
public static void initConfig()Initializes Naftah configuration by loading default and optional external property files.The initialization process follows these steps:
- Load default configuration properties from the resource file
CONFIG_FILE. - Apply built-in configuration values via
processConfig(Properties). - If a system property
CONFIG_FILE_PROPERTYis defined, attempt to load external configuration from the specified file. External values override defaults. - If loading fails (e.g., due to
NaftahBugError), fall back to default properties.
All successfully processed properties update system properties, including
BUILTIN_CLASSES_PROPERTYandBUILTIN_PACKAGES_PROPERTY. - Load default configuration properties from the resource file
-
waitForUserInterruption
private static void waitForUserInterruption()Blocks until the user presses Ctrl+C, using the REPL terminal and line reader.This is a convenient wrapper around
REPLHelper.waitForUserInterruption(LineReader), automatically obtaining the terminal and reader.All other input is ignored; the user sees a friendly Arabic message and can exit gracefully.
-
processArgs
Processes the command line arguments and dispatches the appropriate command.- Parameters:
args- the raw command line arguments
-
printPaddedToString
Prints the string representation of the given object with padding applied.- Parameters:
o- the object to convert to a string and print with padding
-
printPaddedErrorMessageToString
Prints the error message of the given throwable with padding applied. If the throwable is not an instance ofNaftahBugError, it will be wrapped inside aNaftahBugError. The error message is formatted in Arabic script as "تم التقاط الخطأ: 'message'".- Parameters:
t- the throwable whose error message will be printed with padding
-
run
Runs the given NaftahCommand.- Parameters:
naftahCommand- the command to run- Returns:
- true if successful, false otherwise
-