/modules/freetype2/builds/atari/deflinejoiner.awk

http://github.com/zpao/v8monkey · AWK · 181 lines · 142 code · 38 blank · 1 comment · 31 complexity · 31e0a62bf6df66f3b70b2805f55ecd72 MD5 · raw file

  1. #!/usr/bin/env awk
  2. function shift( array, \
  3. junk, elm0, l )
  4. {
  5. elm0 = array[0]
  6. for ( l = 0; l < asorti( array, junk ) - 1; l++ )
  7. array[l] = array[l+1];
  8. delete array[l]
  9. return elm0
  10. }
  11. function init_cpp_src_line()
  12. {
  13. logical_line = ""
  14. delete break_pos
  15. }
  16. function shift_valid_bp( array, \
  17. junk, elm )
  18. {
  19. elm = -1
  20. if ( 0 < asorti( array, junk ) )
  21. do {
  22. elm = shift( array )
  23. } while ( 0 > elm );
  24. return elm
  25. }
  26. function check_cpp_src_line_break_pos( \
  27. i, junk )
  28. {
  29. printf( "break_pos:" )
  30. for ( i = 0; i < asorti( break_pos, junk ); i++ )
  31. printf( " %d", break_pos[i] );
  32. printf( "\n" )
  33. }
  34. function check_cpp_src_line()
  35. {
  36. printf( "logical_line[%s]\n", logical_line )
  37. check_cpp_src_line_break_pos()
  38. }
  39. function append_line( phys_line, \
  40. filt_line, bp_len )
  41. {
  42. filt_line = phys_line
  43. sub( /\\$/, " ", filt_line )
  44. logical_line = logical_line filt_line
  45. bp_len = asorti( break_pos, junk )
  46. break_pos[bp_len] = length( logical_line ) - 1
  47. }
  48. function print_line( \
  49. c0, c1, i, junk, part_str )
  50. {
  51. c0 = 0
  52. while( asorti( break_pos, junk ) > 1 )
  53. {
  54. if ( ( c1 = shift_valid_bp( break_pos ) ) < 1 )
  55. {
  56. part_str = substr( logical_line, c0 + 1 )
  57. printf( "%s\n", part_str )
  58. return
  59. }
  60. part_str = substr( logical_line, c0 + 1, c1 - c0 + 1 )
  61. gsub( / $/, "\\", part_str )
  62. printf( "%s\n", part_str )
  63. c0 = c1 + 1
  64. }
  65. part_str = substr( logical_line, c0 + 1 )
  66. printf( "%s\n", part_str )
  67. }
  68. function shrink_spaces( pos, \
  69. tail, removed_length, k )
  70. {
  71. tail = substr( logical_line, pos )
  72. sub( /^[ \t]+/, " ", tail )
  73. removed_length = length( logical_line ) - pos - length( tail ) + 1
  74. logical_line = substr( logical_line, 0, pos - 1 ) tail
  75. for ( k = 0; k < asorti( break_pos, junk ); k++ )
  76. if ( ( pos + removed_length ) <= break_pos[k] )
  77. break_pos[k] = break_pos[k] - removed_length;
  78. else if ( pos <= break_pos[k] )
  79. break_pos[k] = -1;
  80. return removed_length
  81. }
  82. function shrink_spaces_to_linebreak( pos, \
  83. junk, part_str, removed_length, i )
  84. {
  85. for ( i = 0; i < asorti( break_pos, junk ) && break_pos[i] < pos ; i++ )
  86. ;
  87. if ( break_pos[i] < 1 )
  88. return;
  89. part_str = substr( logical_line, pos, break_pos[i] - pos + 1 )
  90. sub( /^[ \t]+/, " ", part_str )
  91. removed_length = ( break_pos[i] - pos + 1 ) - length( part_str )
  92. tail = substr( logical_line, pos + removed_length )
  93. logical_line = substr( logical_line, 0, pos - 1 ) tail
  94. for ( ; i < asorti( break_pos, junk ); i++ )
  95. break_pos[i] -= removed_length;
  96. return removed_length
  97. }
  98. function delete_linebreaks_in_2nd_token( \
  99. tail, paren_depth, junk, i, j, k, l )
  100. {
  101. if ( logical_line ~ /^[ \t]*#[ \t]*define[ \t]+[0-9A-Za-z_]+\(/ )
  102. {
  103. tail = logical_line
  104. sub( /^[ \t]*#[ \t]*define[ \t]+[0-9A-Za-z_]+/, "", tail )
  105. paren_depth = 0
  106. l = 0
  107. i = length( logical_line ) - length( tail ) + 1 # seek to the 1st op paren
  108. j = i
  109. do {
  110. if ( substr( logical_line, j, 2 ) ~ /[ \t][ \t]/ )
  111. l = shrink_spaces( j );
  112. else if ( substr( logical_line, j, 1 ) == "(" )
  113. paren_depth += 1;
  114. else if ( substr( logical_line, j, 1 ) == ")" )
  115. paren_depth -= 1;
  116. j += 1
  117. } while ( j < length( logical_line ) && paren_depth != 0 )
  118. for ( k = 0; k < asorti( break_pos, junk ); k++ )
  119. if ( i <= break_pos[k] && break_pos[k] < j )
  120. break_pos[k] = -1;
  121. if ( l > 0 )
  122. shrink_spaces_to_linebreak( j );
  123. }
  124. }
  125. BEGIN{
  126. init_cpp_src_line()
  127. }
  128. {
  129. append_line( $0 )
  130. if ( $0 !~ /\\$/ )
  131. {
  132. delete_linebreaks_in_2nd_token()
  133. print_line()
  134. init_cpp_src_line()
  135. }
  136. }
  137. END{
  138. if ( 0 < length( logical_line ) )
  139. {
  140. delete_linebreaks_in_2nd_token()
  141. print_line()
  142. }
  143. }