Package com.esotericpig.jeso.botbuddy
Class BotBuddyCode
java.lang.Object
com.esotericpig.jeso.botbuddy.BotBuddyCode
- All Implemented Interfaces:
Closeable,AutoCloseable
BotBuddyCode is a simple scripting "language" forBotBuddy. It is not Turing complete. The idea was to make a very simple parser, without including the overhead of Groovy/JRuby into Jeso. It can handle Ruby-like string literals and heredoc, and simple methods (no params). It can accept the following input: -BufferedReader-Path[usePaths.get(...)] -List<String> usingStringListReader-StringusingStringReaderExample usage with a file:try(BotBuddyCode bbc = BotBuddyCode.builder(Paths.get("file.txt")).build()) { // Don't execute any code, just output result of interpreting: System.out.println(bbc.interpretDryRun()); }Example usage with a list of strings:List<String> list = new LinkedList<>(); list.add("puts 'Hello World'"); list.add(""); list.add("get_coords"); try(BotBuddyCode bbc = BotBuddyCode.builder(list).build()) { // Interpret and execute code bbc.interpret(); }Example of functionality:# This is a comment puts <<EOS # Heredoc Hello World EOS # End tag can be indented puts <<-EOS # ltrim to min indent Hello World EOS paste 592 254 <<-EOS # Heredoc with other args Hello World EOS # Method names are flexible begin_safe_mode endSafeMode # Quoted strings can also have newlines puts "Hello \" World\"" puts 'Hello \'World\'' # Special quotes like Ruby, where you choose the terminator puts %(Hello \) World) puts %^Hello \^ World^ # Define your own (user) method # - Cannot take in args def my_method get_coords get_pixel 1839 894 printscreen # Saves file to current directory getOSFamily end # Can call multiple methods in one line call my_method myMethodReal world example:puts "Get ready..." delay 2000 begin_safe_mode paste 1187 492 "Sakana" paste 1450 511 "Fish" click 1851 1021 delay_long paste 1187 492 "Niku" paste 1450 511 "Meat" click 1851 1021 end_safe_mode
- Author:
- Jonathan Bradley Whited
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic interfacestatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BotBuddyprotected StringBuilderprotected intstatic final intstatic final intprotected intprotected BotBuddyCode.Executorsprotected booleanprotected booleanprotected booleanprotected BufferedReaderstatic final Stringprotected Stringprotected Stringprotected intprotected intprotected intprotected ReadWriteLockprotected StringBuilderprotected Map<String,BotBuddyCode.UserMethod> static final Pattern -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddUserMethod(BotBuddyCode.Instruction instruction) static BotBuddyCode.Builderbuilder()static BotBuddyCode.Builderbuilder(BufferedReader input) static BotBuddyCode.Builderbuilder(CharSequence str) static BotBuddyCode.Builderstatic BotBuddyCode.Builderstatic BotBuddyCode.BuilderbuildParseCodeException(String message) buildParseCodeException(String message, Throwable cause) voidcallUserMethod(BotBuddyCode.Instruction instruction) voidclose()voidexecute(BotBuddyCode.Instruction instruction) getBuddy()intintbooleanhadCode()booleanhadInput()booleanbooleanvoidinterpret(boolean execute) booleanbooleanbooleanisReady()nextLine()intvoidoutput(BotBuddyCode.Instruction instruction) voidoutput(BotBuddyCode.Instruction instruction, String prefix) voidoutputWithIndent(BotBuddyCode.Instruction instruction) readQuote(int endQuote) booleanvoidvoidsetCommentChar(int commentChar) voidsetEscapeChar(int escapeChar) voidsetExecutors(BotBuddyCode.Executors executors)
-
Field Details
-
DEFAULT_COMMENT_CHAR
public static final int DEFAULT_COMMENT_CHAR- See Also:
-
DEFAULT_ESCAPE_CHAR
public static final int DEFAULT_ESCAPE_CHAR- See Also:
-
INSTRUCTION_CALL_ID
- See Also:
-
WHITESPACE_PATTERN
-
buddy
-
buffer
-
commentChar
protected int commentChar -
escapeChar
protected int escapeChar -
executors
-
hadCode
protected boolean hadCode -
hadInput
protected boolean hadInput -
hadInstruction
protected boolean hadInstruction -
input
-
instructionName
-
line
-
lineChar
protected int lineChar -
lineIndex
protected int lineIndex -
lineNumber
protected int lineNumber -
lock
-
output
-
userMethods
-
-
Constructor Details
-
BotBuddyCode
- Throws:
AWTExceptionIOException
-
-
Method Details
-
builder
-
builder
-
builder
-
builder
-
builder
-
builder
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
addUserMethod
public BotBuddyCode.UserMethod addUserMethod(BotBuddyCode.Instruction instruction) throws ParseCodeException - Throws:
ParseCodeException
-
buildParseCodeException
-
buildParseCodeException
-
callUserMethod
- Throws:
ParseCodeException
-
execute
- Throws:
ParseCodeException
-
interpret
- Throws:
IOExceptionParseCodeException
-
interpret
- Throws:
IOExceptionParseCodeException
-
interpretDryRun
- Throws:
IOExceptionParseCodeException
-
nextLine
- Throws:
IOException
-
nextLineChar
public int nextLineChar() -
output
-
output
-
outputWithIndent
-
readHeredoc
- Throws:
IOExceptionParseCodeException
-
readQuote
- Throws:
IOException
-
readSpecialQuote
- Throws:
IOExceptionParseCodeException
-
readToEndOfLine
-
readToWhitespace
-
seekToNonWhitespace
public boolean seekToNonWhitespace()- Returns:
- true if found a non-whitespace char, else false
-
setBuddy
-
setCommentChar
public void setCommentChar(int commentChar) -
setEscapeChar
public void setEscapeChar(int escapeChar) -
setExecutors
-
getBuddy
-
hadCode
public boolean hadCode() -
getCommentChar
public int getCommentChar() -
isCommentChar
public boolean isCommentChar() -
isEndOfLine
public boolean isEndOfLine() -
getEscapeChar
public int getEscapeChar() -
getExecutors
-
hadInput
public boolean hadInput() -
hadInstruction
public boolean hadInstruction() -
hasLineChar
public boolean hasLineChar() -
isReady
- Throws:
IOException
-