2009-03-11 15:08:26 -04:00
|
|
|
import time
|
2009-03-10 17:12:24 -04:00
|
|
|
from mode import Fundamental
|
|
|
|
from lex import Grammar, PatternRule, RegionRule, NocasePatternRule, NocaseRegionRule
|
2009-03-10 22:58:22 -04:00
|
|
|
from mode.python import StringGrammar2
|
2009-03-11 15:08:26 -04:00
|
|
|
from method.shell import Interact
|
2009-03-10 17:12:24 -04:00
|
|
|
|
|
|
|
class DataGrammar(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'[^)]+')]
|
|
|
|
|
2009-03-10 22:58:22 -04:00
|
|
|
class StringGrammar1(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'[^\']+')]
|
|
|
|
class StringGrammar3(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'[^)]+')]
|
|
|
|
|
2009-03-10 17:12:24 -04:00
|
|
|
class ForthGrammar(Grammar):
|
|
|
|
rules = [
|
2009-03-11 15:08:26 -04:00
|
|
|
PatternRule(r'comment', r"\\(?: .*)?\n$"),
|
|
|
|
RegionRule(r'comment', r'\((?= |\n)', DataGrammar, r'\)'),
|
2009-03-10 17:12:24 -04:00
|
|
|
NocaseRegionRule(r'comment', r'0 \[if\]', DataGrammar, r'\[(?:endif|then)\]'),
|
|
|
|
PatternRule(r'delimiter', r"[:;\[\]]"),
|
2009-03-10 22:58:22 -04:00
|
|
|
RegionRule(r'string', r'[.cs]" ', StringGrammar1, r'"'),
|
|
|
|
RegionRule(r'string', r'[.s]\\" ', StringGrammar2, r'"'),
|
2009-03-11 15:08:26 -04:00
|
|
|
RegionRule(r'string', r'\.\( ', StringGrammar3, r'\)'),
|
|
|
|
PatternRule(r'operator', r'(?:mod|m\*|f\+|f-|f\*|f/|\+|-|\*/mod|\*/|\*|/mod|/)(?=[ \n$])'),
|
2009-03-10 22:58:22 -04:00
|
|
|
NocasePatternRule(r'keyword', r'(?:variable|value|until|tuck|truth|to|then|test|swap|step|see|rot|roll|recurse|r>|pick|over|mod|m\*|loop|locals|invert|if|hex|here|fsort|fln|fexp|f!|f@|f\+|f-|f\*|f/|execute|endif|else|dup|drop|does>|do|decimal|d!|d@|create|constant|cells|cell|c!|c@|bye|begin|allot|\[(?:if|then|endif)\]|>r|!|@|\+loop|\+|-|\*/mod|\*/|\*|/mod|/)(?=[ \n$])'),
|
2009-03-10 17:12:24 -04:00
|
|
|
PatternRule(r'forth_def', r'(?<=:) +[^ ]+'),
|
|
|
|
NocasePatternRule(r'number', r"'[a-z]"),
|
|
|
|
NocasePatternRule(r'number', r'%?-?[0-1]+\.?'),
|
|
|
|
NocasePatternRule(r'number', r'[&#]?-?[0-9]+\.?'),
|
|
|
|
NocasePatternRule(r'number', r"(?:0x|\$)[0-9a-f]+\.?"),
|
|
|
|
NocasePatternRule(r'number', r'[+-]?[0-9]+\.?e?[+-][0-9]+'),
|
|
|
|
PatternRule(r'forth_word', r'[^ ]+'),
|
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
|
|
|
]
|
|
|
|
|
2009-03-11 15:08:26 -04:00
|
|
|
class GforthStart(Interact):
|
|
|
|
args = []
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
Interact._execute(self, w, bname='*GForth*', cmd='gforth')
|
|
|
|
class GforthLoadFile(Interact):
|
|
|
|
args = []
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
Interact._execute(self, w, bname='*GForth*', cmd='gforth')
|
|
|
|
b = w.application.get_buffer_by_name('*GForth*')
|
|
|
|
time.sleep(0.1)
|
|
|
|
for line in w.buffer.lines:
|
|
|
|
b.pipe_write(line + '\n')
|
|
|
|
|
2009-03-10 17:12:24 -04:00
|
|
|
class Forth(Fundamental):
|
|
|
|
modename = 'FORTH'
|
2009-03-10 22:58:22 -04:00
|
|
|
extensions = ['.fs', '.fi', '.fb']
|
2009-03-10 17:12:24 -04:00
|
|
|
grammar = ForthGrammar
|
|
|
|
commentc = '\\'
|
|
|
|
colors = {
|
|
|
|
'forth_def': ('blue', 'default', 'bold'),
|
|
|
|
'forth_word': ('yellow', 'default', 'bold'),
|
|
|
|
}
|
2009-03-11 15:08:26 -04:00
|
|
|
actions = [GforthStart, GforthLoadFile]
|
2009-03-10 17:12:24 -04:00
|
|
|
|
|
|
|
install = Forth.install
|