Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hexadecimal and binary literals #429

Closed
ChristianGruen opened this issue Apr 5, 2023 · 9 comments
Closed

Hexadecimal and binary literals #429

ChristianGruen opened this issue Apr 5, 2023 · 9 comments
Labels
Feature A change that introduces a new feature XPath An issue related to XPath

Comments

@ChristianGruen
Copy link
Contributor

Without wanting to challenge our weekly burn-down chart too much, I wonder whether it would be a big deal to support hexadecimal and binary literals in XPath? Examples:

(: decimal :)     1, 255,
(: hexadecimal :) 0x1, 0X00Ff,
(: binary :)      0b1, 0B11111111, 

The main question is probably if it conflicts with the existing grammar?

@ChristianGruen ChristianGruen added XPath An issue related to XPath Feature A change that introduces a new feature labels Apr 5, 2023
@michaelhkay
Copy link
Contributor

Related to issue #241. One of the issues here is with the sign bit. In languages with fixed-length integers, 0xFFFFFFFF means -1 because we know there are 32 bits; with a variable-length integer, that doesn't work, and it's going to throw people if we adopt the familiar syntax with unfamiliar semantics.

@ChristianGruen
Copy link
Contributor Author

Related to issue #241. One of the issues here is with the sign bit. In languages with fixed-length integers, 0xFFFFFFFF means -1 because we know there are 32 bits; with a variable-length integer, that doesn't work, and it's going to throw people if we adopt the familiar syntax with unfamiliar semantics.

I would propose to do the same what I summarized in #241 (comment): Most languages (including JavaScript, Python, Perl, or PHP) interpret 0xFFFFFFFF as 4294967295, and it would be surprising to get -1 in XPath (even more because there’s no fixed 32 bit xs:integer limit). And if an XPath implementation uses 32 bits for xs:integer, I would expect 0xFFFFFFFF to be rejected with err:FOAR0002.

@innovimax
Copy link
Contributor

This ticket if I'm correct is about changing how XPath parses number
#241 is more about adding few function in F&O or EXPath or ThinLayer:tm:

@ndw
Copy link
Contributor

ndw commented Apr 6, 2023

If we have functions to do the conversions, and I think we should, adding the ability to write literals in hex and binary seems unnecessary to me. The compiler can obviously evaluate parse-integer-or-whatever-we-call-it('01011101', 2) as, uh, 93 I think, at compile time so there's no actual difference between that and also allowing 0b01011101 except additional complexity.

@ChristianGruen
Copy link
Contributor Author

ChristianGruen commented Apr 6, 2023

I think it does make a difference, as it's much easier to write 0xF instead of parse-integer('0F', 16). Instead of using the function, I believe users would rather compute the decimal value in advance and use a comment to indicate the hexadecimal counterpart.

It's still helpful to have additional functions, as they allow you to interpret dynamic input.

@ChristianGruen
Copy link
Contributor Author

I’m getting tenacious (I beg pardon), but I believe XPath and XQuery are the only languages I have worked with so far that don’t support hexadecimal/binary literals. If it turns out that the addition is more complex than I would hope, though, I agree we should focus on the existing to-dos.

@line-o
Copy link
Contributor

line-o commented Apr 6, 2023

I side with @ChristianGruen and am in favour of adding additional literals that are common to other programming languages.
I also have a use case: Function annotations in XQuery would benefit from the ability to have additional literal value types.

@rhdunn
Copy link
Contributor

rhdunn commented Apr 9, 2023

I would always interpret the hexadecimal/binary literals as unsigned/positive values -- more specifically as xs:integer values, which don't specify a given size outside of implementation defined limits. Note that this is in line with the existing decimal literals, which are also unsigned/positive xs:integer values per the Literals section of the specification:

The value of a numeric literal containing no "." and no e or E character is an atomic value of type xs:integer.

If the user wants a negative value, they can use -0xF like they can with -16. The XPath/XQuery grammar separates out unary minus/negation from the numeric literal construction.

@ndw
Copy link
Contributor

ndw commented Apr 25, 2023

Closed by #433

@ndw ndw closed this as completed Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A change that introduces a new feature XPath An issue related to XPath
Projects
None yet
Development

No branches or pull requests

6 participants