Skip to content

Commit

Permalink
[frontend refactor] Remove old span ID wrappers from location.py!
Browse files Browse the repository at this point in the history
This was pretty easy
  • Loading branch information
Andy C committed May 20, 2023
1 parent 132575b commit cead4c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 57 deletions.
42 changes: 0 additions & 42 deletions frontend/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,45 +397,3 @@ def RightTokenForWord(w):
raise AssertionError(w.tag())

raise AssertionError('for -Wreturn-type in C++')



#
# Wrappers to remove
#


def OfWordPartLeft(part):
# type: (word_part_t) -> int
"""
Span ID wrapper to remove
"""
tok = LeftTokenForWordPart(part)
if tok:
return tok.span_id
else:
return runtime.NO_SPID


def OfWordLeft(w):
# type: (word_t) -> int
"""
TODO: Should return a Token
"""
tok = LeftTokenForWord(w)
if tok:
return tok.span_id
else:
return runtime.NO_SPID


def OfWordRight(w):
# type: (word_t) -> int
"""
Span ID wrapper to remove
"""
tok = RightTokenForWord(w)
if tok:
return tok.span_id
else:
return runtime.NO_SPID
29 changes: 14 additions & 15 deletions tools/osh2oil.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,13 @@ def DoRedirect(self, node, local_symbols):
# Turn everything into <<<. We just change the quotes
self.f.write('<<<')

#here_begin_spid2 = location.OfWordRight(node.here_begin)
if delim_quoted:
self.f.write(" '''")
else:
self.f.write(' """')

delim_end_spid = location.OfWordRight(here_begin)
self.cursor.SkipUntilSpid(delim_end_spid + 1)
delim_end_tok = location.RightTokenForWord(here_begin)
self.cursor.SkipPast(delim_end_tok)

#self.cursor.SkipUntilSpid(here_begin_spid + 1)

Expand Down Expand Up @@ -466,32 +465,33 @@ def DoCommand(self, node, local_symbols, at_top_level=False):
if len(node.words):
first_word = node.words[0]
ok, val, quoted = word_.StaticEval(first_word)
word0_spid = location.OfWordLeft(first_word)
word0_tok = location.LeftTokenForWord(first_word)
if ok and not quoted:
if val == '[':
last_word = node.words[-1]
# Check if last word is ]
ok, val, quoted = word_.StaticEval(last_word)
if ok and not quoted and val == ']':
# Replace [ with 'test'
self.cursor.PrintUntilSpid(word0_spid)
self.cursor.SkipUntilSpid(word0_spid + 1)
self.cursor.PrintUntil(word0_tok)
self.cursor.SkipPast(word0_tok)
self.f.write('test')

for w in node.words[1:-1]:
self.DoWordInCommand(w, local_symbols)

# Now omit ]
last_spid = location.OfWordLeft(last_word)
self.cursor.PrintUntilSpid(last_spid - 1) # Get the space before
self.cursor.SkipUntilSpid(last_spid + 1) # ] takes one spid
rbrack_tok = location.LeftTokenForWord(last_word)
# Skip the space token before ]
self.cursor.PrintUntilSpid(rbrack_tok.span_id - 1)
self.cursor.SkipPast(rbrack_tok) # ] takes one spid
return
else:
raise RuntimeError('Got [ without ]')

elif val == '.':
self.cursor.PrintUntilSpid(word0_spid)
self.cursor.SkipUntilSpid(word0_spid + 1)
self.cursor.PrintUntil(word0_tok)
self.cursor.SkipPast(word0_tok)
self.f.write('source')
return

Expand Down Expand Up @@ -974,10 +974,9 @@ def DoWordInCommand(self, node, local_symbols):
def DoWordPart(self, node, local_symbols, quoted=False):
# type: (word_part_t, Dict[str, bool], bool) -> None

span_id = location.OfWordPartLeft(node)
if span_id != runtime.NO_SPID:
span = self.arena.GetToken(span_id)
self.cursor.PrintUntilSpid(span_id)
left_tok = location.LeftTokenForWordPart(node)
if left_tok:
self.cursor.PrintUntil(left_tok)

UP_node = node

Expand Down

0 comments on commit cead4c6

Please sign in to comment.