foounparser.pm

来自「用于词法分析的词法分析器」· PM 代码 · 共 95 行

PM
95
字号
# $Id: FooUnparser.pm,v 1.3 1996/07/27 07:47:22 matt Exp $## Foo unparser utility## (c) June 96 Matt Phillips.require 'FooObject.pm';package FooUnparser;# unparses object to file.sub unparse{  my ($file, $object) = @_;  doObject ($file, $object, 0);}sub doObject{  my ($file, $object, $indent) = @_;  if ($object->isAtomic ())  {    my $value = $object->getAttr ();    # change control/binary chars into \ddd form    $value =~ s/([\x00-\x20\x80-\xff])/sprintf ("\\%02x", ord ($1))/ge;    # escape any "'s    $value =~ s/\"/\\\"/g;    print $file ("\"$value\"");  } else  {    my $first = 1;		# true if at first attribute    print $file ('( ');    # output each attribute    for $attr ($object->getAttrNames ())    {      newlineAndIndent ($file, $indent + 1) if !$first;      $first = 0;      doAttr ($file, $attr, $object->getAttrs ($attr), $indent + 1);    }    newlineAndIndent ($file, $indent);    print $file (')')  }}sub doAttr{  my ($file, $attr, $values, $indent) = @_;  print $file ($attr);  if ($#$values == 0)  {				# no []'s if single value for attr    my $value = $$values[0];    if ($value->isAtomic ())    {				# put value on same line if atomic      print $file ("\t");    } else    {      newlineAndIndent ($file, $indent);    }    doObject ($file, $value, $indent);  } else  {				# use [] form for values    my $first = 1;    newlineAndIndent ($file, $indent);    print $file ('[ ');    for $value (@$values)    {      newlineAndIndent ($file, $indent + 1) if !$first;      $first = 0;      doObject ($file, $value, $indent + 1);    }    newlineAndIndent ($file, $indent);    print $file (']');  }}sub newlineAndIndent{  my ($file, $indent) = @_;  print $file ("\n", '  'x$indent);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?