# JOE syntax highlight file for Python

=Idle
=Ident
=Comment
=Docstring	+Comment
=DocstringLabel	+Docstring
=DocEscape	+Escape +Docstring
=Constant
=String		+Constant
=Number		+Constant
=Boolean	+Constant
=StringEscape	+Escape
=Keyword
=Statement	+Keyword
=Loop		+Statement
=Conditional	+Statement
=StorageClass	+Type +Keyword
=Operator	+Keyword
=Bad
=Brace

=DefinedIdent	+Ident
=DefinedFunction +DefinedIdent
=DefinedType	+DefinedIdent
=SpecialIdent	+Ident
=Decorator	+Define +Preproc
=Builtin	+DefinedFunction

=FstringBrace	+Brace

:idle Idle
	*		idle
	"#"		comment		recolor=-1
	"0-9"		first_digit	recolor=-1
	"\i"		ident		noeat
	"'\""		string_quot_1	recolor=-1 push_c
.ifdef fstring
.ifdef docstring
	&		doc_end_quot_1	recolor=-1 push_c
.else
	&		idle		noeat return
.endif
	":"		idle		noeat return
	"{"		fstring_brace	recolor=-1
	"}"		idle		noeat return
.else
	"{}"		brace		recolor=-1
.endif
	"@"		decorator	recolor=-1

.ifdef fstring
:fstring_brace FstringBrace
	*		return_from_expr	noeat call=python()

:return_from_expr FstringBrace
	*		return_from_expr	noeat return
	"}"		idle

.ifdef docstring
:doc_end_quot_1 String string
	*		string_quot_1	noeat
	&		doc_end_quot_2	pop_c

:doc_end_quot_2 String string
	*		idle		noeat
	&		idle		noeat return
.endif
.endif

:brace Brace
	*		idle		noeat

# annotations
:decorator Decorator
	*		decorator
	" (\t\r\n"	idle		noeat

:comment Comment comment
	*		comment
	# might be TODO label
	"BFHNTX"	comment		noeat call=comment_todo.comment_todo()
	"\n"		idle

:first_digit Number
	*		idle		noeat
	"xX"		hex
	"."		float
	"eE"		epart
	"0-9"		first_number

:hex Number
	*		idle		noeat
	"0-9A-Fa-f"	hex

:first_number Number
	*		idle		noeat
	"0-9"		first_number
	"."		float

:maybe_float Number
	*		idle		recolor=-2	noeat
	"0-9"		float		recolor=-2

:float Number
	*		idle		noeat
	"eE"		epart
	"0-9"		float

:epart Number
	*		idle		noeat
	"0-9+\-"	enum

:enum Number
	*		idle		noeat
	"0-9"		enum

:ident Ident
	*		ident1		noeat buffer
	"_"		priv_member	recolor=-1

:ident1 Ident
	*		idle		noeat strings
	"and"		oper
	"as"		stmt
	"assert"	stmt
	"async"		defstmt
	"await"		stmt
	"break"		loop
	"case"		cond
	"class"		classstmt
	"continue"	loop
	"def"		defstmt
	"del"		stmt
	"elif"		cond
	"else"		cond
	"except"	stmt
	"finally"	stmt
	"for"		loop
	"from"		stmt
	"global"	stmt
	"if"		cond
	"import"	stmt
	"in"		oper
	"is"		oper
	"lambda"	kw
	"match"		cond
	"nonlocal"	stmt
	"not"		oper
	"or"		oper
	"pass"		stmt
	"raise"		stmt
	"return"	stmt
	"try"		stmt
	"while"		loop
	"with"		stmt
	"yield"		stmt

	# quasi-keywords
	"False"		bool
	"True"		bool
	"None"		kw

	# builtins
	"abs"		builtin
	"all"		builtin
	"any"		builtin
	"ascii"		builtin
	"bin"		builtin
	"bool"		builtin
	"bytearray"	builtin
	"bytes"		builtin
	"callable"	builtin
	"chr"		builtin
	"classmethod"	builtin
	"compile"	builtin
	"complex"	builtin
	"copyright"	builtin
	"credits"	builtin
	"delattr"	builtin
	"dict"		builtin
	"dir"		builtin
	"divmod"	builtin
	"enumerate"	builtin
	"eval"		builtin
	"exec"		builtin
	"exit"		builtin
	"filter"	builtin
	"float"		builtin
	"format"	builtin
	"frozenset"	builtin
	"getattr"	builtin
	"globals"	builtin
	"hasattr"	builtin
	"hash"		builtin
	"help"		builtin
	"hex"		builtin
	"id"		builtin
	"input"		builtin
	"int"		builtin
	"isinstance"	builtin
	"issubclass"	builtin
	"iter"		builtin
	"len"		builtin
	"license"	builtin
	"list"		builtin
	"locals"	builtin
	"map"		builtin
	"max"		builtin
	"memoryview"	builtin
	"min"		builtin
	"next"		builtin
	"object"	builtin
	"oct"		builtin
	"open"		builtin
	"ord"		builtin
	"pow"		builtin
	"print"		builtin
	"property"	builtin
	"quit"		builtin
	"range"		builtin
	"repr"		builtin
	"reversed"	builtin
	"round"		builtin
	"set"		builtin
	"setattr"	builtin
	"slice"		builtin
	"sorted"	builtin
	"staticmethod"	builtin
	"str"		builtin
	"sum"		builtin
	"super"		builtin
	"tuple"		builtin
	"type"		builtin
	"vars"		builtin
	"zip"		builtin

done
	"'\""		idle 		noeat istrings
	"u"		string_pre
	"b"		string_pre
	"r"		string_raw_pre
	"br"		string_raw_pre
	"rb"		string_raw_pre
	"f"		string_fstring_pre
	"fr"		string_raw_fstring_pre
	"rf"		string_raw_fstring_pre
done
	"\c"		ident1

:kw Keyword
	*		idle		noeat

:cond Conditional
	*		idle		noeat

:oper Operator
	*		idle		noeat

:bool Boolean
	*		idle		noeat

:loop Loop
	*		idle		noeat

:stmt Statement
	*		idle		noeat

:builtin Builtin
	*		idle		noeat

:classstmt Statement
	*		idle 		noeat recolor=-1
	"\i"		classstmt
	" \t"		classname_1

:classname_1 DefinedType
	*		classname

:classname DefinedType
	*		idle		noeat recolor=-1
	"\c"		classname

:defstmt Statement
	*		idle		noeat recolor=-1
	"\i"		defstmt
	" \t"		defname_1

:defname_1 DefinedFunction
	*		defname

:defname DefinedFunction
	*		idle		noeat recolor=-1
	"\c"		defname

:priv_member SpecialIdent
	*		idle		noeat
	"\c"		priv_member

# Handle string prefixes up to the string itself.
:string_pre String string
	*		idle noeat
	"'\""		string_quot_1 	push_c

:string_raw_pre String string
	*		idle noeat
	"'\""		string_quot_raw_1 push_c

:string_fstring_pre String string
	*		idle noeat
	"'\""		string_quot_fstring_1 	push_c

:string_raw_fstring_pre String string
	*		idle noeat
	"'\""		string_quot_raw_fstring_1 push_c

# Differentiate between docstrings and regular strings, carrying with it raw state
:string_quot_1 String string
	*		idle call=.string(-docstring) noeat
	&		string_quot_2

:string_quot_2 String string
	*		idle noeat
	&		idle call=.string(docstring) recolor=-3

:string_quot_raw_1 String string
	*		idle call=.string(-docstring raw)
	&		string_quot_raw_2

:string_quot_raw_2 String string
	*		idle noeat
	&		idle call=.string(docstring raw) recolor=-3

:string_quot_fstring_1 String string
	*		idle call=.string(-docstring fstring) noeat
	&		string_quot_fstring_2

:string_quot_fstring_2 String string
	*		idle noeat
	&		idle call=.string(docstring fstring) recolor=-3

:string_quot_raw_fstring_1 String string
	*		idle call=.string(-docstring raw fstring)
	&		string_quot_raw_fstring_2

:string_quot_raw_fstring_2 String string
	*		idle noeat
	&		idle call=.string(docstring raw fstring) recolor=-3

.subr string

.ifdef docstring	# Long strings
:string Docstring string
	*		string
	"BFHNTX"	string		noeat call=.comment_todo_docstr()
.ifdef raw
.else
	"\\"		string_esc	mark
.endif
.ifdef fstring
	"{"		fstring_maybe_expr	recolor=-1
	"}"		fstring_esc_or_bad	recolor=-1 mark
.endif
	&		doc_end_1

:doc_end_1 Docstring string
	*		string noeat
	&		doc_end_2

:doc_end_2 Docstring string
	*		string noeat
	&		string return pop_c

.else			# Short strings

:string String string
	*		string
	"\n"		string return pop_c
.ifdef raw
.else
	"\\"		string_esc mark
.endif
.ifdef fstring
	"{"		fstring_maybe_expr	recolor=-1
	"}"		fstring_esc_or_bad	recolor=-1 mark
.endif
	&		string return pop_c

.endif

:string_esc String string
	*		string_esc_done
	"x"		string_hex2
	"u"		string_hex4
	"U"		string_hex8
	"0-7"		string_octal2
	"\n"		string_esc_done
.ifdef fstring
	"{"		fstring_esc_maybe_expr	recolor=-1
.endif

.ifdef fstring
:fstring_esc_maybe_expr FstringBrace
	*		fstring_maybe_expr	noeat
	"{"		string_esc_really_done	recolor=-2

:fstring_maybe_expr FstringBrace
	*		fstring_maybe_end	noeat call=python()
	"{"		string_esc_really_done	recolor=-2

:fstring_maybe_end FstringBrace
	*		string			noeat
.ifdef docstring
	&		doc_end_2		noeat recolor=-3
.endif
	"}"		string
	":"		fstring_format

:fstring_format Constant
	*		fstring_format
	"{"		fstring_format_fstring	recolor=-1
	"}"		fstring_maybe_end	noeat

:fstring_format_fstring FstringBrace
	*		fstring_format_fstring_maybe_end	noeat call=python()

:fstring_format_fstring_maybe_end FstringBrace
	*		string			noeat
.ifdef docstring
	&		doc_end_2		noeat recolor=-3
.endif
	"}"		fstring_format
	":"		fstring_format_fstring_format

:fstring_format_fstring_format Constant
	*		fstring_format_fstring_format
	"{"		fstring_esc_or_bad			recolor=-1 # Too deep
	"}"		fstring_format_fstring_format_end	recolor=-1

:fstring_format_fstring_format_end FstringBrace
	*		fstring_format		noeat
.endif

# Recolor whole escape sequence based on whether this is a docstring.
:string_esc_done String string
	*		string_esc_really_done noeat markend recolormark

.ifdef fstring
:fstring_esc_or_bad Bad string
	*		string			noeat
	"}"		fstring_esc

:fstring_esc String string
	*		string_esc_really_done	noeat markend recolormark
.endif

.ifdef docstring
:string_esc_really_done DocEscape
	*		string 		noeat
.else
:string_esc_really_done StringEscape string
	*		string 		noeat
.endif

:string_hex1 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_esc_done

:string_hex2 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex1

:string_hex3 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex2

:string_hex4 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex3

:string_hex5 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex4

:string_hex6 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex5

:string_hex7 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex6

:string_hex8 StringEscape string
	*		string_esc_done	noeat
	"0-9a-fA-F"	string_hex7

:string_octal1 StringEscape string
	*		string_esc_done	noeat
	"0-7"		string_esc_done

:string_octal2 StringEscape string
	*		string_esc_done	noeat
	"0-7"		string_octal1

.end

.subr comment_todo_docstr
# initial state
:comment_todo_init Docstring string
	*		comment_todo_guess	buffer

# highlight common TODO labels
:comment_todo_guess Docstring string
	*		comment_todo_unknown	noeat strings
	"BUG"		comment_todo
	"FIXME"		comment_todo
	"HACK"		comment_todo
	"NOTE"		comment_todo
	"TODO"		comment_todo
	"XXX"		comment_todo
done
	"A-Z"		comment_todo_guess

:comment_todo_unknown Docstring string
	*		NULL		noeat return

:comment_todo DocstringLabel string
	*		NULL		noeat return
.end
