old-style attributes are still available but getters and setters are manual.
This encourage the use of new-style attributes.
Support for old style attributes will be removed eventually.
Signed-off-by: Jean Privat <jean@pryen.org>
#
# This file is free software, which comes along with NIT. This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. You can modify it is you want, provided this header
# is kept unaltered, and a notification of the changes is added.
# You are allowed to redistribute it and sell it, alone or is a part of
class DummyArray
super Set[Int]
super ArrayCapable[Int]
- var _capacity: Int
- redef readable var _length: Int
+ var _capacity: Int
+ var _length: Int
+ redef fun length do return _length
var _keys: NativeArray[Int]
var _values: NativeArray[Int]
class FilterIStream
super IStream
# Filter readed elements
- readable var _stream: nullable IStream = null
+ var stream: nullable IStream = null
redef fun eof: Bool
do
assert stream != null
return stream.eof
end
-
- private fun stream=(i: nullable IStream)
- do
- _stream = i
- end
end
class FilterOStream
super OStream
# Filter outputed elements
- readable var _stream: nullable OStream = null
+ var stream: nullable OStream = null
# Can the stream be used to write
redef fun is_writable: Bool
assert stream != null
return stream.is_writable
end
-
- private fun stream=(i: OStream)
- do
- _stream = i
- end
end
class StreamCat
redef fun stream: nullable IStream
do
- if _stream == null and _streams.is_ok then
- stream = _streams.item
- assert _stream != null
+ var res = super
+ if res == null and _streams.is_ok then
+ res = _streams.item
+ stream = res
+ assert stream != null
_streams.next
end
- return _stream
+ return res
end
redef fun read_char: Int
#
# This file is free software, which comes along with NIT. This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. You can modify it is you want, provided this header
# is kept unaltered, and a notification of the changes is added.
# You are allowed to redistribute it and sell it, alone or is a part of
# Super class of all option's class
abstract class Option
# Names for the option (including long and short ones)
- readable var _names: Array[String]
+ var names: Array[String]
# Type of the value of the option
type VALUE: nullable Object
# Human readable description of the option
- readable var _helptext: String
+ var helptext: String
# Gathering errors during parsing
- readable var _errors: Array[String]
+ var errors: Array[String]
# Is this option mandatory?
- readable writable var _mandatory: Bool
+ var mandatory: Bool writable
# Has this option been read?
- readable var _read:Bool
+ var read:Bool writable
# Current value of this option
- writable var _value: nullable VALUE
-
- # Current value of this option
- fun value: VALUE do return _value.as(VALUE)
+ var value: VALUE writable
# Default value of this option
- readable writable var _default_value: nullable VALUE
+ var default_value: nullable VALUE writable
# Create a new option
init init_opt(help: String, default: nullable VALUE, names: nullable Array[String])
do
if names == null then
- _names = new Array[String]
+ self.names = new Array[String]
else
- _names = names.to_a
+ self.names = names.to_a
end
- _helptext = help
- _mandatory = false
- _read = false
- _default_value = default
- _value = default
- _errors = new Array[String]
+ helptext = help
+ mandatory = false
+ read = false
+ default_value = default
+ value = default
+ errors = new Array[String]
end
# Add new aliases for this option
- fun add_aliases(names: String...) do _names.add_all(names)
+ fun add_aliases(names: String...) do names.add_all(names)
# An help text for this option with default settings
redef fun to_s do return pretty(2)
fun pretty(off: Int): String
do
var text = new FlatBuffer.from(" ")
- text.append(_names.join(", "))
+ text.append(names.join(", "))
text.append(" ")
var rest = off - text.length
if rest > 0 then text.append(" " * rest)
# Consume parameters for this option
protected fun read_param(it: Iterator[String])
do
- _read = true
+ read = true
end
end
protected fun convert(str: String): VALUE is abstract
# Is the parameter mandatory?
- readable writable var _parameter_mandatory: Bool
+ var parameter_mandatory: Bool writable
redef fun read_param(it)
do
value = convert(it.item)
it.next
else
- if _parameter_mandatory then
- _errors.add("Parameter expected for option {names.first}.")
+ if parameter_mandatory then
+ errors.add("Parameter expected for option {names.first}.")
end
end
end
init init_opt(h, d, n)
do
super
- _parameter_mandatory = true
+ parameter_mandatory = true
end
end
class OptionEnum
super OptionParameter
redef type VALUE: Int
- var _values: Array[String]
+ var values: Array[String]
init(values: Array[String], help: String, default: Int, names: String...)
do
assert values.length > 0
- _values = values.to_a
+ self.values = values.to_a
init_opt("{help} <{values.join(", ")}>", default, names)
end
redef fun convert(str)
do
- var id = _values.index_of(str)
+ var id = values.index_of(str)
if id == -1 then
- var e = "Unrecognized value for option {_names.join(", ")}.\n"
- e += "Expected values are: {_values.join(", ")}."
- _errors.add(e)
+ var e = "Unrecognized value for option {names.join(", ")}.\n"
+ e += "Expected values are: {values.join(", ")}."
+ errors.add(e)
end
return id
end
redef fun pretty_default
do
if default_value != null then
- return " ({_values[default_value.as(not null)]})"
+ return " ({values[default_value.as(not null)]})"
else
return ""
end
init(help: String, names: String...)
do
- _values = new Array[String]
- init_opt(help, _values, names)
+ values = new Array[String]
+ init_opt(help, values, names)
end
- var _values: Array[String]
+ private var values: Array[String]
redef fun convert(str)
do
- _values.add(str)
- return _values
+ values.add(str)
+ return values
end
end
class OptionContext
- readable var _options: Array[Option]
- readable var _rest: Array[String]
- readable var _errors: Array[String]
+ var options: Array[Option]
+ var rest: Array[String]
+ var errors: Array[String]
- var _optmap: Map[String, Option]
+ private var optmap: Map[String, Option]
fun usage
do
var lmax = 1
- for i in _options do
+ for i in options do
var l = 3
for n in i.names do
l += n.length + 2
if lmax < l then lmax = l
end
- for i in _options do
+ for i in options do
print(i.pretty(lmax))
end
end
do
var parseargs = true
build
- var rest = _rest
+ var rest = rest
while parseargs and it.is_ok do
var str = it.item
var next_called = false
for i in [1..str.length] do
var short_opt = "-" + str.chars[i].to_s
- if _optmap.has_key(short_opt) then
- var option = _optmap[short_opt]
+ if optmap.has_key(short_opt) then
+ var option = optmap[short_opt]
if option isa OptionParameter then
it.next
next_called = true
end
if not next_called then it.next
else
- if _optmap.has_key(str) then
- var opt = _optmap[str]
+ if optmap.has_key(str) then
+ var opt = optmap[str]
it.next
opt.read_param(it)
else
end
end
- for opt in _options do
+ for opt in options do
if opt.mandatory and not opt.read then
- _errors.add("Mandatory option {opt.names.join(", ")} not found.")
+ errors.add("Mandatory option {opt.names.join(", ")} not found.")
end
end
end
fun add_option(opts: Option...)
do
for opt in opts do
- _options.add(opt)
+ options.add(opt)
end
end
init
do
- _options = new Array[Option]
- _optmap = new HashMap[String, Option]
- _rest = new Array[String]
- _errors = new Array[String]
+ options = new Array[Option]
+ optmap = new HashMap[String, Option]
+ rest = new Array[String]
+ errors = new Array[String]
end
private fun build
do
- for o in _options do
+ for o in options do
for n in o.names do
- _optmap[n] = o
+ optmap[n] = o
end
end
end
do
var errors: Array[String] = new Array[String]
- errors.add_all(_errors)
+ errors.add_all(errors)
- for o in _options do
+ for o in options do
for e in o.errors do
errors.add(e)
end
class Container[E]
super Collection[E]
- redef fun first do return _item
+ redef fun first do return item
redef fun is_empty do return false
redef fun length do return 1
- redef fun has(an_item) do return _item == an_item
+ redef fun has(an_item) do return item == an_item
- redef fun has_only(an_item) do return _item == an_item
+ redef fun has_only(an_item) do return item == an_item
redef fun count(an_item)
do
- if _item == an_item then
+ if item == an_item then
return 1
else
return 0
redef fun iterator do return new ContainerIterator[E](self)
# Create a new instance with a given initial value.
- init(e: E) do _item = e
+ init(e: E) do item = e
# The stored item
- readable writable var _item: E
+ var item: E writable
end
# This iterator is quite stupid since it is used for only one item.
super Iterator[E]
redef fun item do return _container.item
- redef fun next do _is_ok = false
+ redef fun next do is_ok = false
init(c: Container[E]) do _container = c
- redef readable var _is_ok: Bool = true
+ redef var is_ok: Bool = true
var _container: Container[E]
end
class Couple[F, S]
# The first element of the couple.
- readable writable var _first: F
+ var first: F writable
# The second element of the couple.
- readable writable var _second: S
+ var second: S writable
# Create a new instance with a first and a second object.
init(f: F, s: S)
do
- _first = f
- _second = s
+ first = f
+ second = s
end
end
abstract class AbstractArrayRead[E]
super SequenceRead[E]
- redef readable var _length: Int = 0
+ var _length: Int = 0
+ redef fun length do return _length
redef fun is_empty do return _length == 0
_index = 0
end
- redef readable var _index: Int = 0
+ var _index: Int = 0
+ redef fun index do return _index
var _array: AbstractArrayRead[E]
end
var _capacity: Int = 0 # Size of _array
var _length: Int = 0 # Number of items in the map
- readable var _first_item: nullable N = null # First added item (used to visit items in nice order)
+ var _first_item: nullable N = null # First added item (used to visit items in nice order)
var _last_item: nullable N = null # Last added item (same)
# The last key accessed (used for cache)
private abstract class HashNode[K: Object]
var _key: K
type N: HashNode[K]
- readable writable var _next_item: nullable N = null
- readable writable var _prev_item: nullable N = null
+ var _next_item: nullable N = null
+ var _prev_item: nullable N = null
var _prev_in_bucklet: nullable N = null
var _next_in_bucklet: nullable N = null
init(k: K)
init(map: HashMap[K, V])
do
_map = map
- _node = map.first_item
+ _node = map._first_item
end
end
var _node: nullable ListNode[E]
# The index of the current node
- redef readable var _index: Int
+ var _index: Int
+
+ redef fun index do return _index
# Remove the current item
fun delete
end
# The next node.
- readable writable var _next: nullable ListNode[E]
+ var next: nullable ListNode[E]
# The previous node.
- readable writable var _prev: nullable ListNode[E]
+ var prev: nullable ListNode[E]
end
class Range[E: Discrete]
super Collection[E]
- redef readable var _first: E
+ redef var first: E
# Get the last element.
- readable var _last: E
+ var last: E
# Get the element after the last one.
- readable var _after: E
+ var after: E
- redef fun has(item) do return item >= _first and item <= _last
+ redef fun has(item) do return item >= first and item <= last
- redef fun has_only(item) do return _first == item and item == _last or is_empty
+ redef fun has_only(item) do return first == item and item == last or is_empty
redef fun count(item)
do
redef fun length
do
- var nb = _first.distance(_after)
+ var nb = first.distance(after)
if nb > 0 then
return nb
else
end
end
- redef fun is_empty do return _first >= _after
+ redef fun is_empty do return first >= after
# Create a range [`from`, `to`].
# The syntax `[from..to[` is equivalent.
init(from: E, to: E)
do
- _first = from
- _last = to
- _after = to.successor(1)
+ first = from
+ last = to
+ after = to.successor(1)
end
# Create a range [`from`, `to`[.
# The syntax `[from..to[` is equivalent.
init without_last(from: E, to: E)
do
- _first = from
- _last = to.predecessor(1)
- _after = to
+ first = from
+ last = to.predecessor(1)
+ after = to
end
end
# Iterator on ranges.
super Iterator[E]
var _range: Range[E]
- redef readable var _item: E
+ var _item: E
+ redef fun item do return _item
redef fun is_ok do return _item < _range.after
abstract class FStream
super IOS
# The path of the file.
- readable var _path: nullable String = null
+ var path: nullable String = null
# The FILE *.
var _file: nullable NativeFile = null
fun reopen
do
if not eof then close
- _file = new NativeFile.io_open_read(_path.to_cstring)
- _end_reached = false
+ _file = new NativeFile.io_open_read(path.to_cstring)
+ end_reached = false
_buffer_pos = 0
_buffer.clear
end
redef fun close
do
var i = _file.io_close
- _end_reached = true
+ end_reached = true
end
redef fun fill_buffer
do
var nb = _file.io_read(_buffer.items, _buffer.capacity)
if nb <= 0 then
- _end_reached = true
+ end_reached = true
nb = 0
end
_buffer.length = nb
end
# End of file?
- redef readable var _end_reached: Bool = false
+ redef var end_reached: Bool = false
# Open the file at `path` for reading.
init open(path: String)
do
- _path = path
+ self.path = path
prepare_buffer(10)
- _file = new NativeFile.io_open_read(_path.to_cstring)
+ _file = new NativeFile.io_open_read(path.to_cstring)
assert cant_open_file: _file != null
end
do
_file = new NativeFile.io_open_write(path.to_cstring)
assert cant_open_file: _file != null
- _path = path
+ self.path = path
_writable = true
end
super IFStream
private init do
_file = new NativeFile.native_stdin
- _path = "/dev/stdin"
+ path = "/dev/stdin"
prepare_buffer(1)
end
super OFStream
private init do
_file = new NativeFile.native_stdout
- _path = "/dev/stdout"
+ path = "/dev/stdout"
_writable = true
end
end
super OFStream
private init do
_file = new NativeFile.native_stderr
- _path = "/dev/stderr"
+ path = "/dev/stderr"
_writable = true
end
end
# Matches are a part of a `Text` found by a `Pattern`.
class Match
# The base string matched
- readable var _string: String
+ var string: String
# The starting position in the string
- readable var _from: Int
+ var from: Int
# The length of the matching part
- readable var _length: Int
+ var length: Int
# The position of the first character just after the matching part.
# May be out of the base string
- fun after: Int do return _from + _length
+ fun after: Int do return from + length
# The contents of the matching part
- redef fun to_s do return _string.substring(_from,_length)
+ redef fun to_s do return string.substring(from,length)
# Matches `len` characters of `s` from `f`.
init(s: String, f: Int, len: Int)
assert positive_length: len >= 0
assert valid_from: f >= 0
assert valid_after: f + len <= s.length
- _string = s
- _from = f
- _length = len
+ string = s
+ from = f
+ length = len
end
end