Class: SyntaxTree::Binary
Overview
Binary represents any expression that involves two sub-expressions with an operator in between. This can be something that looks like a mathematical operation:
1 + 1
but can also be something like pushing a value onto an array:
array << value
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#left ⇒ Object
readonly
- untyped
-
the left-hand side of the expression.
-
#operator ⇒ Object
readonly
- Symbol
-
the operator used between the two expressions.
-
#right ⇒ Object
readonly
- untyped
-
the right-hand side of the expression.
Attributes inherited from Node
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(left:, operator:, right:, location:, comments: []) ⇒ Binary
constructor
A new instance of Binary.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(left:, operator:, right:, location:, comments: []) ⇒ Binary
Returns a new instance of Binary.
1873 1874 1875 1876 1877 1878 1879 |
# File 'lib/syntax_tree/node.rb', line 1873 def initialize(left:, operator:, right:, location:, comments: []) @left = left @operator = operator @right = right @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1871 1872 1873 |
# File 'lib/syntax_tree/node.rb', line 1871 def comments @comments end |
#left ⇒ Object (readonly)
- untyped
-
the left-hand side of the expression
1862 1863 1864 |
# File 'lib/syntax_tree/node.rb', line 1862 def left @left end |
#operator ⇒ Object (readonly)
- Symbol
-
the operator used between the two expressions
1865 1866 1867 |
# File 'lib/syntax_tree/node.rb', line 1865 def operator @operator end |
#right ⇒ Object (readonly)
- untyped
-
the right-hand side of the expression
1868 1869 1870 |
# File 'lib/syntax_tree/node.rb', line 1868 def right @right end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
1881 1882 1883 |
# File 'lib/syntax_tree/node.rb', line 1881 def child_nodes [left, right] end |
#deconstruct_keys(keys) ⇒ Object
1887 1888 1889 1890 1891 1892 1893 1894 1895 |
# File 'lib/syntax_tree/node.rb', line 1887 def deconstruct_keys(keys) { left: left, operator: operator, right: right, location: location, comments: comments } end |
#format(q) ⇒ Object
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 |
# File 'lib/syntax_tree/node.rb', line 1897 def format(q) power = operator == :** q.group do q.group { q.format(left) } q.text(" ") unless power q.group do q.text(operator) q.indent do q.breakable(power ? "" : " ") q.format(right) end end end end |
#pretty_print(q) ⇒ Object
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 |
# File 'lib/syntax_tree/node.rb', line 1915 def pretty_print(q) q.group(2, "(", ")") do q.text("binary") q.breakable q.pp(left) q.breakable q.text(operator) q.breakable q.pp(right) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 |
# File 'lib/syntax_tree/node.rb', line 1932 def to_json(*opts) { type: :binary, left: left, op: operator, right: right, loc: location, cmts: comments }.to_json(*opts) end |