Hey guys, I'm trying to write a grammar file for a MATLAB-like language, and I'm having trouble with the transpose operator. The transpose operator, represented by the single quote, can be used in a variety of different situations:
# Transposing a variable: myMatrix'
# Transposing the result of a function: myFunc( myMatrix )'
# Transposing the result of a parenthetical: (myMatrix - 1)'
# Transposing a matrix literal: [1,2,3]'
The first two forms are not difficult, they can be matched with simple "match" forms. However, the parenthetical and matrix literal forms are more difficult; using begin/end blocks in the pattern matching causes *every *open parentheses or open square bracket to match, regardless of whether or not there is a transpose operator matched with the closing parentheses or square bracket. Not only does this cause performance issues, this causes issues with nested operators such as:
([1,2,3]' - [4,5,6]')
Any ideas on how to do this the proper way? I don't really care about matching the parentheses or the square brackets; I just care that they are present. Positive look-behind assertions would work fabulously here, but they can't be variable-length, so that's a no-go here.
Thanks! -E
I wonder what the scopes you want would look like?
if fun(m)' generates:
<paren><object><paren><transpose>, then all you need to do is catch the transpose: It always mean transpose, right, so just match ' and scope it.
if you want to scope transposed objects (why?) - like this:
<transposedobject><paren><object><paren></transposedobject> Then you could catch many cases with something like this early on in the syntax
([^ ]+)(') and pass the first bit on to source.julia or whatever language it is you are writing for
But if there is no illegal character like white space to help you know what is being transposed, then I think you would need to build it up in multiple rules (probably like the language parser does), seeking for matched pairs of brackets to jump back over legal white space so match (stuff)' with ([^(]+) ?(') (add something to allow an optional function name in front of the (
then a rule for (())' etc.
This won't grid to a halt, as it is not looking backwards for things that never occur, but looking forwards for closing transform tick not too far from brackets that it finds.
best of luck, tim
On 2 Sep 2012, at 01:52, Elliot Saba staticfloat@gmail.com wrote:
Hey guys, I'm trying to write a grammar file for a MATLAB-like language, and I'm having trouble with the transpose operator. The transpose operator, represented by the single quote, can be used in a variety of different situations:
# Transposing a variable: myMatrix'
# Transposing the result of a function: myFunc( myMatrix )'
# Transposing the result of a parenthetical: (myMatrix - 1)'
# Transposing a matrix literal: [1,2,3]'
The first two forms are not difficult, they can be matched with simple "match" forms. However, the parenthetical and matrix literal forms are more difficult; using begin/end blocks in the pattern matching causes every open parentheses or open square bracket to match, regardless of whether or not there is a transpose operator matched with the closing parentheses or square bracket. Not only does this cause performance issues, this causes issues with nested operators such as:
([1,2,3]' - [4,5,6]')
Any ideas on how to do this the proper way? I don't really care about matching the parentheses or the square brackets; I just care that they are present. Positive look-behind assertions would work fabulously here, but they can't be variable-length, so that's a no-go here.
Thanks! -E
_______________________________________________ textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
Thanks for the quick response, Tim!
This will require some testing to make sure there's no chance it could get confused with the single-quoted string type, but I like the simplicity of it. Thanks! -E