### Implementing Custom GrammarLocator - Java Source: https://github.com/noties/prism4j/blob/master/README.md Shows how to create a custom implementation of the `GrammarLocator` interface, which is responsible for providing language grammars to the Prism4j instance based on the requested language identifier string. This example provides the 'json' grammar. ```java public class MyGrammarLocator implements GrammarLocator { @Nullable @Override public Prism4j.Grammar grammar(@NonNull Prism4j prism4j, @NonNull String language) { switch (language) { case "json": return Prism_json.create(prism4j); // everything else is omitted default: return null; } } } ``` -------------------------------- ### Defining a Custom Grammar - Java Source: https://github.com/noties/prism4j/blob/master/README.md Illustrates how to programmatically define a language grammar for Prism4j using static helper methods for creating grammars and tokens with associated regular expression patterns. This example defines the basic syntax rules for JSON. ```java import static java.util.regex.Pattern.CASE_INSENSITIVE; import static java.util.regex.Pattern.compile; import static io.noties.prism4j.Prism4j.grammar; import static io.noties.prism4j.Prism4j.pattern; import static io.noties.prism4j.Prism4j.token; @Aliases("jsonp") public class Prism_json { @NonNull public static Prism4j.Grammar create(@NonNull Prism4j prism4j) { return grammar( "json", token("property", pattern(compile("\"(?:\\\\.|[^\\\\"\\r\\n])*\"(?=\\s*:)", CASE_INSENSITIVE))), token("string", pattern(compile("\"(?:\\\\.|[^\\\\"\\r\\n])*\"(?!\\s*:)"), false, true)), token("number", pattern(compile("\\b0x[\\dA-Fa-f]+\\b|(?:\\b\\d+\\.?\\d*|\\B\\.\\d+)(?:[Ee][+-]?\\d+)?"))), token("punctuation", pattern(compile("[{}\\[\\]);,]"))), token("operator", pattern(compile(":"))), token("boolean", pattern(compile("\\b(?:true|false)\\b", CASE_INSENSITIVE))), token("null", pattern(compile("\\bnull\\b", CASE_INSENSITIVE))) ); } } ``` -------------------------------- ### Running Prism4j Language Tests - Bash Source: https://github.com/noties/prism4j/blob/master/README.md Execute this command in the project's root directory to run the Gradle task specifically responsible for testing the language definitions within the `prism4j-languages` module. This verifies that ported grammars correctly tokenize sample code. ```bash ./gradlew :prism4j-languages:test ``` -------------------------------- ### Using Prism4j for Tokenization - Java Source: https://github.com/noties/prism4j/blob/master/README.md Demonstrates how to initialize the Prism4j library with a custom grammar locator, retrieve a specific language grammar, tokenize an input string of code, and then traverse the resulting syntax nodes using a visitor pattern. ```java final Prism4j prism4j = new Prism4j(new MyGrammarLocator()); final Grammar grammar = prism4j.grammar("json"); if (grammar != null) { final List nodes = prism4j.tokenize(code, grammar); final AbsVisitor visitor = new AbsVisitor() { @Override protected void visitText(@NonNull Prism4j.Text text) { // raw text text.literal(); } @Override protected void visitSyntax(@NonNull Prism4j.Syntax syntax) { // type of the syntax token syntax.type(); visit(syntax.children()); } }; visitor.visit(nodes); } ``` -------------------------------- ### Configuring Language Bundling - Java Source: https://github.com/noties/prism4j/blob/master/README.md Apply the `@PrismBundle` annotation to any class in your project to specify which pre-ported languages should be included by the bundler and to define the name of the generated `GrammarLocator` class. The `includes` array lists the desired languages by their real names. ```java @PrismBundle( includes = { "clike", "java", "c" }, grammarLocatorClassName = ".MyGrammarLocator" ) public class MyClass {} ``` -------------------------------- ### Adding Prism4j Bundler Dependency - Gradle Source: https://github.com/noties/prism4j/blob/master/README.md Include the `prism4j-bundler` annotation processor dependency in your project's build file. This module automatically generates `GrammarLocator` implementations based on annotations, simplifying the inclusion of pre-ported language grammars. ```kotlin annotationProcessor 'io.noties:prism4j-bundler:${prism_version}' ``` -------------------------------- ### Adding Prism4j Core Dependency - Gradle Source: https://github.com/noties/prism4j/blob/master/README.md Add the core Prism4j library dependency to your project's build file using Gradle/Kotlin DSL. This artifact provides the main tokenization API classes without any built-in language definitions, which need to be provided separately. ```kotlin implementation "io.noties:prism4j:${prism_version}" ``` -------------------------------- ### Extending a Base Language with @Extend - Java Source: https://github.com/noties/prism4j/blob/master/README.md Apply the `@Extend` annotation to a language definition class to declare that it extends another language. When bundled, the base language (e.g., "clike") will automatically be included and processed before the extending language, even if not explicitly listed in `@PrismBundle` includes. ```java @Extend("clike") public class Prism_c {} ``` -------------------------------- ### Defining Language Aliases with @Aliases - Java Source: https://github.com/noties/prism4j/blob/master/README.md Use the `@Aliases` annotation on a language definition class to associate one or more alternative names with the language. This allows the grammar to be retrieved using any of its aliases via the `Prism4j.grammar()` method. ```java @Aliases({"html", "xml", "mathml", "svg"}) public class Prism_markup {} ``` -------------------------------- ### Modifying an Existing Language with @Modify - Java Source: https://github.com/noties/prism4j/blob/master/README.md Use the `@Modify` annotation on a language definition class to indicate that it modifies another language's grammar rules. When the modified language is requested and present, the modifying language's rules will be applied after the base grammar is loaded. ```java @Modify("markup") public class Prism_css {} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.