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
-String
usingStringReader
Example 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 myMethod
Real 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 class
static class
static class
static interface
static class
static class
static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BotBuddy
protected StringBuilder
protected int
static final int
static final int
protected int
protected BotBuddyCode.Executors
protected boolean
protected boolean
protected boolean
protected BufferedReader
static final String
protected String
protected String
protected int
protected int
protected int
protected ReadWriteLock
protected StringBuilder
protected Map<String,
BotBuddyCode.UserMethod> static final Pattern
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddUserMethod
(BotBuddyCode.Instruction instruction) static BotBuddyCode.Builder
builder()
static BotBuddyCode.Builder
builder
(BufferedReader input) static BotBuddyCode.Builder
builder
(CharSequence str) static BotBuddyCode.Builder
static BotBuddyCode.Builder
static BotBuddyCode.Builder
buildParseCodeException
(String message) buildParseCodeException
(String message, Throwable cause) void
callUserMethod
(BotBuddyCode.Instruction instruction) void
close()
void
execute
(BotBuddyCode.Instruction instruction) getBuddy()
int
int
boolean
hadCode()
boolean
hadInput()
boolean
boolean
void
interpret
(boolean execute) boolean
boolean
boolean
isReady()
nextLine()
int
void
output
(BotBuddyCode.Instruction instruction) void
output
(BotBuddyCode.Instruction instruction, String prefix) void
outputWithIndent
(BotBuddyCode.Instruction instruction) readQuote
(int endQuote) boolean
void
void
setCommentChar
(int commentChar) void
setEscapeChar
(int escapeChar) void
setExecutors
(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:
AWTException
IOException
-
-
Method Details
-
builder
-
builder
-
builder
-
builder
-
builder
-
builder
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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:
IOException
ParseCodeException
-
interpret
- Throws:
IOException
ParseCodeException
-
interpretDryRun
- Throws:
IOException
ParseCodeException
-
nextLine
- Throws:
IOException
-
nextLineChar
public int nextLineChar() -
output
-
output
-
outputWithIndent
-
readHeredoc
- Throws:
IOException
ParseCodeException
-
readQuote
- Throws:
IOException
-
readSpecialQuote
- Throws:
IOException
ParseCodeException
-
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
-